Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL: One of the provided types for building the Schema is missing a name

I'm learning GraphQL so I got a strange issue

I have this code on one file Schema.js:

const graphQL = require('graphql');
const lodash = require('lodash')
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;

const StatusType = new GraphQLObjectType({
name: 'Status',
fields: () => ({
    id: { type: GraphQLInt },
    statusName: { type: GraphQLString },
    user: {
        type: new GraphQLList(UserType),
        resolve(parentValue, args){
            
        }
    }
})
});

const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
    id: { type: GraphQLString },
    username: { type: GraphQLString },
    mail: { type: GraphQLString },
    password: { type: GraphQLString },
    status: { 
        type: StatusType,
        resolve(parentValue, args){
            
        }
    },
})
});

const RouteQuery = new GraphQLObjectType({
name: 'RouteQuery',
user: {
        type: UserType,
        args: { id: { type: GraphQLString } },
        resolve(parentValue, args){
            //return lodash.find(users, { id: args.id })
        }
    },
userSome: {
        type: new GraphQLList(UserType),
        args: { id: { type: GraphQLString } },
        resolve(parentValue, args){
            if (args.id) {
                //return users.filter(user => user.id === args.id);
            }
            //return users;
        }
    },
userAll: {
        type: new GraphQLList(UserType),
        resolve(parentValue){
            //return users
        }
    },
status:{
        type: StatusType,
        args: { id: { type: GraphQLInt } },
        resolve(parentValue, args){
            //return lodash.find(status, { id: args.id })
        }
    },
statusAll: {
        type: new GraphQLList(StatusType),
        resolve(parentValue){
            //return users
        }
    }
    }
});

module.exports = new GraphQLSchema({
query: RouteQuery
})

This code run succesfully but when i try to separate these into multiple files: the const StatusType & UserType like the following case: the StatusType is on StatusType.js file and the UserType is on UserType.js file

StatuType.js file:

const graphQL = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;
const UserType = require('./UserType')
const StatusType = new GraphQLObjectType({
name: 'Status',
fields: () => ({
    id: { type: GraphQLInt },
    statusName: { type: GraphQLString },
    user: {
        type: new GraphQLList(UserType),
        resolve(parentValue, args){
            //return users.filter(user => user.status === parentValue.id);
        }
    }
})
});
module.exports = StatusType;

UserType.js file:

const graphQL = require('graphql');
const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLID, GraphQLSchema, GraphQLList } = graphQL;
const StatusType = require('./StatusType')

const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
    id: { type: GraphQLString },
    username: { type: GraphQLString },
    mail: { type: GraphQLString },
    password: { type: GraphQLString },
    status: { 
        type: StatusType,
        resolve(parentValue, args){
            //return lodash.find(status, { id: parentValue.status })
        }
    },
})
});
module.exports = UserType;

And on the Schema.js file i include these 2 like that:

const StatusType = require('./StatusType');
const UserType = require('./UserType');

so instead of putting the all code on the same file, i putted the StatusType and UserType on respective files.

but when i run this code, i got this error:

enter image description here

So i don't know what the problem here :/

But when i'm tring to console.log the const UserType = require('./UserType') i got User as response :o like when it was on the same code on Schema.js

like image 904
Michel Gisenaël Avatar asked Mar 02 '23 13:03

Michel Gisenaël


2 Answers

You are facing a problem in the way nodeJs handle require. See http://nodejs.org/api/modules.html#modules_cycles for how require is handled in node.

Specifically in your case, when you do:

const StatusType = require('./StatusType');
const UserType = require('./UserType');
  1. StatusType is loaded from const StatusType = require('./StatusType');
  2. StatusType.js loads UserType from const UserType = require('./UserType')
  3. UserType.js should require StatusType but nodeJs prevent this to avoid infinite loop. As a result, it executes next lines
  4. UserType is initialized as new GraphQLObjectType(...) and defined fields as a function. The function closure hand a variable StatusType not yet initialized. It's just an empty exported module {}

You can verify that adding console.log(StatusType); when creating UserType fields:

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => {
    console.log(StatusType);
    return ({
      id: { type: GraphQLString },
      username: { type: GraphQLString },
      mail: { type: GraphQLString },
      password: { type: GraphQLString },
      status: {
        type: StatusType,
        resolve(parentValue, args) {

        }
      },
    });
  }
});

You'll get:

{} //instead of StatusType

You didn't encounter this problem when everything was in the same file because both UserType and StatusType are defined within the same closure and now each others.

To resolve that you had to define UserType and StatusType on the same level and inject them. A good example of how to do it can be found here. In your case:

// StatusType.js
const StatusType = (types) => new GraphQLObjectType({
  name: 'Status',
  fields: () => {
    console.log(types.UserType);
    return ({
      id: { type: GraphQLInt },
      statusName: { type: GraphQLString },
      user: {
        type: new GraphQLList(types.UserType),
        resolve(parentValue, args) {

        }
      }
    });
  }
});

module.exports = StatusType;

// UserType.js
const UserType = (types) => new GraphQLObjectType({
  name: 'User',
  fields: () => {
    console.log(types.StatusType);
    return ({
      id: { type: GraphQLString },
      username: { type: GraphQLString },
      mail: { type: GraphQLString },
      password: { type: GraphQLString },
      status: {
        type: types.StatusType,
        resolve(parentValue, args) {

        }
      },
    });
  }
});
module.exports = UserType;

// Schema.js
const StatusTypeInject = require('./StatusType');
const UserTypeInject = require('./UserType');

const types = {};
types.StatusType = StatusTypeInject(types);
types.UserType = UserTypeInject(types);

const StatusType = types.StatusType;
const UserType = types.UserType;

like image 184
Damien Leroux Avatar answered Mar 05 '23 15:03

Damien Leroux


You could do with some cleaning up here and here's how I'd resolve these situations:

[..]
// import GraphQLNonNull from the graphql lib
// In your case, I'd use GraphQLID instead of GraphQLString

userSome: {
  type: new GraphQLList(require('../path/to/UserType')),
  args: { id: { type: new GraphQLNonNull(GraphQLID) } },
  resolve: async (parentValue, args) => {
    // No need for the if statement as we'd sure to have an id.
    // return await filter users by id.
  }
},
[..]

And as always, keep your fields as functions: fields: () => ({})

like image 32
Sylar Avatar answered Mar 05 '23 16:03

Sylar