leebenson/graphql-with-sequelize

Need help with referencing types when they have yet to be defined in code order

johhansantana opened this issue · 2 comments

Hello, I'm having trouble getting this code to work

const articleType = new GraphQLObjectType({
  name: 'Article',
  fields: {
    id: {
      type: GraphQLInt
    },
    name: {
      type: GraphQLString
    },
    description: {
      type: GraphQLString
    },
    createdBy: {
      type: userType,
      resolve(article) {
        return article.findUser();
      }
    }
  }
});

const userType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: {
      type: GraphQLInt
    },
    name: {
      type: GraphQLString
    },
    lastName: {
      type: GraphQLString

    },
    email: {
      type: GraphQLString
    },
    createdAt: {
      type: GraphQLString
    },
    articles: {
      type: new GraphQLList(articleType),
      resolve(user) {
        return user.getArticles();
      }
    }
  }
});

in articleType I'm trying to use userType but when I run the server it errrors out with Article.createdBy field type must be Output Type but got: undefined.

How can I fix this :l

oi, nvm, I have to change the fields object into a thunk function like so

const articleType = new GraphQLObjectType({
  name: 'Article',
  fields: () => ({
    id: {
      type: GraphQLInt
    },
    name: {
      type: GraphQLString
    },
    description: {
      type: GraphQLString
    },
    createdBy: {
      type: userType,
      resolve(article) {
        return article.findUser();
      }
    }
  })
});

👍