mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

usage with apollo-server v2 #651

Closed sajadghawami closed 5 years ago

sajadghawami commented 5 years ago

Hey guys,

so i am trying to get apollo-server v2 to work with the field helper to use attributeFields. I used this before:

const typeDefs = `
  type User {
    id: ID!
    firstName: String!
    lastName: String!
  }

  type Query {
    users: [User!]!
    user(id: ID!): User
  }
`;

const resolvers = {
  Query: {
    users: (parent, args, { db }, info) => db.user.findAll(),
    user: (parent, { id }, { db }, info) => db.user.findById(id),
  },
};

const server = new ApolloServer({
  typeDefs: gql(typeDefs),
  resolvers,
  context: { db },
});

which worked just fine.

Now, using the following syntax:


const userType = new GraphQLObjectType({
  name: 'user',
  description: 'A user',
  fields: attributeFields(user),
});

// schema
const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      users: {
        type: new GraphQLList(userType),
        resolve: resolver(user),
      },
    },
  }),
});

const server = new ApolloServer({
  typeDefs: gql(schema),
  resolvers,
  context: { db },
});

Gives me TypeError: Cannot read property 'replace' of undefined.

Any help would be appreciated! Thanks in advance :)

mickhansen commented 5 years ago

I don't have any experience with apollo-server version 2, you'll have to apply some basic debugging to the stack trace to figure out what is happening.

(Including the full stack of an error is always helpful)

sajadghawami commented 5 years ago

thank you for the quick reply!

logging the above const schema gives me the following:


 GraphQLSchema {
     astNode: undefined,
     extensionASTNodes: undefined,
     _queryType: Query,
     _mutationType: undefined,
     _subscriptionType: undefined,
     _directives: [ @include, @skip, @deprecated ],
     _typeMap:
      { Query: Query,
        user: user,
        Int: Int,
        String: String,
        Boolean: Boolean,
        Date: Date,
        __Schema: __Schema,
        __Type: __Type,
        __TypeKind: __TypeKind,
        __Field: __Field,
        __InputValue: __InputValue,
        __EnumValue: __EnumValue,
        __Directive: __Directive,
        __DirectiveLocation: __DirectiveLocation },
     _implementations: {},
     _possibleTypeMap: undefined,
     __validationErrors: undefined,
     __allowedLegacyNames: [] }

which obviously seems to be the problem.

thats the stack-trace:

 TypeError: Cannot read property 'replace' of undefined
     at normalize (/backend/node_modules/graphql-tag/lib/graphql-tag.umd.js:14:17)
     at parseDocument (/backend/node_modules/graphql-tag/lib/graphql-tag.umd.js:129:18)
     at gql (/backend/node_modules/graphql-tag/lib/graphql-tag.umd.js:176:10)
     at Object.<anonymous> (/backend/src/app.js:45:13)
     at Module._compile (module.js:641:30)
     at Object.Module._extensions..js (module.js:652:10)
     at Module.load (module.js:560:32)
     at tryModuleLoad (module.js:503:12)
     at Function.Module._load (module.js:495:3)
     at Module.require (module.js:585:17)
     at require (internal/module.js:11:18)
     at Object.<anonymous> (/backend/src/bin/www:11:13)
     at Module._compile (module.js:641:30)
     at Object.Module._extensions..js (module.js:652:10)
     at Module.load (module.js:560:32)
     at tryModuleLoad (module.js:503:12)
     at Function.Module._load (module.js:495:3)
     at Function.Module.runMain (module.js:682:10)
     at startup (bootstrap_node.js:191:16)
     at bootstrap_node.js:613:3
mickhansen commented 5 years ago

No part of the stack trace goes through graphql-sequelize, have you verified the issue is actually with attributeFields? Does the error not occur if you manually define userType?

sajadghawami commented 5 years ago

Thanks for your help, i striped out apollo for now and this seems to work!