2017-07-07 2 views
3

Ich versuche meinen ersten GraphQL-Ansatz. Hier ist der Code:GraphQL: Der Feldtyp Query.type muss Ausgabetyp sein, aber: undefined

schema.js:

import { 
    GraphQLSchema, 
    GraphQLObjectType, 
    GraphQLInputObjectType, 
    GraphQLNonNull, 
    GraphQLString, 
    GraphQLBoolean, 
    GraphQLInt, 
    GraphQLID, 
    GraphQLList 
} from 'graphql'; 

import Company from '../../models/Company'; 

const CompanyType = new GraphQLObjectType({ 
    name: 'Company', 
    description: 'Company', 
    fields: { 
     _id: { 
      type: new GraphQLNonNull(GraphQLID) 
     }, 
     name: { 
      type: GraphQLString 
     } 
    } 
}) 


const Companies = { 
    type: CompanyType, 
    args: { 
     id: { 
      name: 'ID', 
      type: new GraphQLNonNull(GraphQLID) 
     } 
    }, 
    resolve(root, params) { 
     return Company.find(params.id).exec(); 
    } 
} 


export default new GraphQLSchema({ 

    query: new GraphQLObjectType({ 
     name: 'Query', 
     fields: Companies 
    }) 
}); 

Dann auf meinem server.js:

import express from 'express'; 
import bodyParser from 'body-parser'; 
import mongoose from 'mongoose'; 
import morgan from 'morgan'; 
import graphqlHTTP from 'express-graphql'; 

import schema from './schema'; 

mongoose.Promise = global.Promise; 

// set up example server 
const app = express(); 
app.set('port', (process.env.API_PORT || 3001)); 


// logger 
app.use(morgan('dev')); 

// parse body 
app.use(bodyParser.json()); 

// redirect all requests to /graphql 
app.use(function redirect(req, res) { 
    res.redirect('/graphql'); 
}); 


app.use('/graphql', graphqlHTTP({ 
    schema: schema, 
    graphqli: true, 
    pretty: true 
})); 

Ich erhalte die folgende errror:

D:\test\node_modules\graphql\jsutils\invariant.js:19 
    throw new Error(message); 
    ^

Error: Query.type field type must be Output Type but got: undefined. 
    at invariant (D:\test\node_modules\graphql\jsutils\invariant.js:19:11) 
    at D:\test\node_modules\graphql\type\definition.js:361:29 
    at Array.forEach (native) 
    at defineFieldMap (D:\test\node_modules\graphql\type\definition.js:352:14) 
    at GraphQLObjectType.getFields (D:\test\node_modules\graphql\type\definition.js:306:44) 
    at typeMapReducer (D:\test\node_modules\graphql\type\schema.js:206:25) 
    at Array.reduce (native) 
    at new GraphQLSchema (D:\test\node_modules\graphql\type\schema.js:95:34) 
    at Object.<anonymous> (D:/9. DEV/WORKSPACE/mom/client/graphql/index.js:62:16) 
    at Module._compile (module.js:570:32) 
    at loader (D:\test\node_modules\babel-register\lib\node.js:144:5) 
    at Object.require.extensions.(anonymous function) [as .js] (D:\test\node_modules\babel-register\lib\node.js:154:7) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.require (module.js:497:17) 

Hilfe geschätzt.

Antwort

2

Innerhalb der Query-Typ-Definition, Sie schreibt:

fields: Companies 

Es sollte am docs Suche

fields: { Companies } 

sein, nehmen Felder ein Objekt oder eine Funktion ein Objekt zurückkehrt, mit dem Objekt des Schlüssel Zuordnung zu den Namen aller Felder des Typs (in diesem Fall alle Abfragen in Ihrem Schema).

Auch aus Gründen der Klarheit, da Abfragen und Mutationen Felder ihrer jeweiligen Typen sind, kann es besser sein, sie in camelCase statt PascalCase zu benennen (d. H. {Companies: Companies}

Verwandte Themen