Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking Missing Object Prototype #127

Closed
patotoma opened this issue Sep 7, 2016 · 10 comments
Closed

Mocking Missing Object Prototype #127

patotoma opened this issue Sep 7, 2016 · 10 comments

Comments

@patotoma
Copy link

patotoma commented Sep 7, 2016

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.

@helfer
Copy link
Contributor

helfer commented Sep 7, 2016

@sebastienbarre could this be related to #115?

@sebastienbarre
Copy link
Contributor

@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...

@helfer
Copy link
Contributor

helfer commented Sep 7, 2016

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

@patotoma
Copy link
Author

patotoma commented Sep 7, 2016

@helfer I did. Hope it helps.

@DxCx
Copy link
Contributor

DxCx commented Sep 8, 2016

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?

@DxCx
Copy link
Contributor

DxCx commented Sep 8, 2016

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

@helfer
Copy link
Contributor

helfer commented Sep 9, 2016

@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.

@DxCx
Copy link
Contributor

DxCx commented Sep 9, 2016

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

@DxCx
Copy link
Contributor

DxCx commented Sep 24, 2016

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

@qertis
Copy link

qertis commented Nov 15, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants