2016-06-13 2 views
3

У меня естьGraphQL ошибка - аргумент типа должен быть Input Type, но получил не определено

export const getPicture = { 
    type: GraphPicture, 
    args: { 
     type: new GraphQLNonNull(GraphQLInt) 
    }, 
    resolve(_, args) { 
     return Picture.findByPrimary(args.id); 
    } 
}; 

export const getPictureList = { 
    type: new GraphQLList(GraphPicture), 
    resolve(_, __, session) { 
     return Picture.findAll({where: {owner: session.userId}}); 
    } 
}; 

и

const query = new GraphQLObjectType({ 
    name: 'Queries', 
    fields:() => { 
     return { 
      getPictureList: getPictureList, 
      getPicture: getPicture 
     } 
    } 
}); 

const schema = new GraphQLSchema({ 
    query: query, 
    mutation: mutation 
}); 

Какие броски:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined. 

getPictureList работает отлично, getPicture терпит неудачу.

Я пробовал сделать getPicture GraphQLLIst, не помогает.

Я пробовал сделать тип args типа ввода, не помогает.

Любые идеи о том, что происходит здесь

трассировки стека является:

Error: Queries.getPicture(type:) argument type must be Input Type but got: undefined. 
at invariant (/home/jrootham/dev/trytheseon/node_modules/graphql/jsutils/invariant.js:19:11) 
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:307:33 
at Array.map (native) 
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:304:52 
at Array.forEach (native) 
at defineFieldMap (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:293:14) 
at GraphQLObjectType.getFields (/home/jrootham/dev/trytheseon/node_modules/graphql/type/definition.js:250:46) 
at /home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:224:27 
at typeMapReducer (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:236:7) 
at Array.reduce (native) 
at new GraphQLSchema (/home/jrootham/dev/trytheseon/node_modules/graphql/type/schema.js:104:34) 
at Object.<anonymous> (/home/jrootham/dev/trytheseon/server/graphQL.js:45:16) 
at Module._compile (module.js:399:26) 
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5) 
at Object.require.extensions.(anonymous function) [as .js] (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:173:7) 
at Module.load (module.js:345:32) 
at Function.Module._load (module.js:302:12) 
at Module.require (module.js:355:17) 
at require (internal/module.js:13:17) 
at Object.<anonymous> (/home/jrootham/dev/trytheseon/devServer.js:14:44) 
at Module._compile (module.js:399:26) 
at normalLoader (/usr/lib/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5) 
+0

Не уверен, что это связано, но вы указали только тип и, похоже, t o не указывать имя аргумента (id). – helfer

+0

@OP, параметризовать 'getPicture' в корневом запросе. В другой заметке вы можете рассмотреть именование 'picture' вместо' getPicture'. Это скорее конвенция. –

+0

Запросы @AhmadFerdous, переданные в определение схемы, не обязательно должны быть параметризованы, они передаются параметрами через сам запрос. –

ответ

9

Проблема не возвращаемый тип запроса, но типа вы указали своим арг.

export const getPicture = { 
    type: GraphPicture, 
    args: { 
     type: new GraphQLNonNull(GraphQLInt) 
    }, 
    resolve(_, args) { 
     return Picture.findByPrimary(args.id); 
    } 
}; 

Предполагая, что вы имели в виду, чтобы иметь аргумент с именем "типа" это должно быть:

export const getPicture = { 
    type: GraphPicture, 
    args: { 
     type: { 
      type: new GraphQLNonNull(GraphQLInt) 
     } 
    }, 
    resolve(_, args) { 
     return Picture.findByPrimary(args.id); 
    } 
}; 

вот пример из graphql-Js репо:

hero: { 
    type: characterInterface, 
    args: { 
    episode: { 
     description: 'If omitted, returns the hero of the whole saga. If ' + 
        'provided, returns the hero of that particular episode.', 
     type: episodeEnum 
    } 
    }, 
    resolve: (root, { episode }) => getHero(episode), 
}, 

Посмотреть полный схема из graphql-js repo здесь: https://github.com/graphql/graphql-js/blob/master/src/tests/starWarsSchema.js