Graphql-tools: Mocking Missing Object Prototype

Created on 7 Sep 2016  路  10Comments  路  Source: ardatan/graphql-tools

When I tried this mocking example there was a problem with missing prototype on Object types.

Wrapper data object is okay but none of the child objects have its prototype attached. See picture below:
screen shot 2016-09-07 at 10 41 49

Objects like user and list items in lists array does not contain proto object.
Arrays are okay with its prototypes.

This is causing errors like:
Uncaught TypeError: Cannot convert object to primitive value
for example when I try to console.log() user object.

external-bug

Most helpful comment

i had to dig the code there for something else and spotted the bug..
so..opened a PR with a bugfix :)
graphql/graphql-js#504

All 10 comments

@sebastienbarre could this be related to #115?

@helfer I'm on the road for some time but if you can come up with a failing test I'll sure look at it...

@patotoma Could you make a PR with a failing test? That will help us fix the bug faster.

@helfer I did. Hope it helps.

hi @helfer , @sebastienbarre
just to let you know that i've had the feeling that this is not related to graphql-tools, so i decided to build the same exact query using pure graphql-js:

  it('GraphQL inherits', () => {
      const Thread = new GraphQLObjectType({
          name: 'Thread',
          fields: {
              id: {
                  type: GraphQLString,
                  resolve(thread, args, ctx) {
                      return thread.id;
                  },
              },
              name: {
                  type: GraphQLString,
                  resolve() {
                      return 'Lorem Lipsum';
                  },
              },
          },
      });
      const Query = new GraphQLObjectType({
          name: 'Query',
          fields: {
              thread: {
                  type: Thread,
                  args: {
                      id: {
                          type: GraphQLString,
                      },
                  },
                  resolve(root, args) {
                      return {id: args.id};
                  }
              },
          },
      });
      const jsSchema = new GraphQLSchema({
          query: Query,
      });
      const testQuery = `query abc{
          thread(id: "67"){
              id
                  name
          }
      }`;
      const expected = {
          thread: {
              id: '67',
              name: 'Lorem Ipsum',
          },
      };
      return graphql(jsSchema, testQuery).then((res) => {
          expect(Object.getPrototypeOf(res.data.thread)).to.deep.equal(
                  Object.getPrototypeOf(expected)
                  );
      });
  });

and as i thought, it yields the same result:

 1) Mock inherits object prototype for objects:
     AssertionError: expected null to deeply equal {}
  2) Mock GraphQL inherits:
     AssertionError: expected null to deeply equal {}

Then i tried to think why other people did not come up with this, and found out that
running JSON.parse(JSON.stringify(res)) fixes prototyings, so usually the response is being sent over,
seriealized, using JSON, so the other end get's a fixed result.

so, the bug itself is on graphql-js constructing code,
@helfer, how do you want to progress with this?

@patotoma , note that you can use the JSON.parse(JSON.stringify(res)) workaround in the meantime.

@DxCx oh, interesting. Thanks for getting to the bottom of this! This is somewhat unexpected, so I think it's worth filing on graphql-js and keep this issue here open in the meantime.

@helfer, done - graphql/graphql-js#484

i had to dig the code there for something else and spotted the bug..
so..opened a PR with a bugfix :)
graphql/graphql-js#504

Please reopen this issue.
http://graphql.org/blog/mocking-with-graphql/ needs server.query instead mockServer.query

Was this page helpful?
0 / 5 - 0 ratings