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

Graphql Mongo db connector ID failure #457

Closed
ZachHaber opened this issue Sep 13, 2018 · 9 comments
Closed

Graphql Mongo db connector ID failure #457

ZachHaber opened this issue Sep 13, 2018 · 9 comments

Comments

@ZachHaber
Copy link

I tried to follow the examples (which are out of date, so I tried looking through the different packages to get things working). I managed to get apollo server's playground running queries and mutations, but this error occurs. For some reason mongodb's ObjectId type doesn't seem to be working correctly.
Any help as to why this is happening would be appreciated. I can post the server code that I put together if it helps. I've also tried setting the mongo connector's "convertSession/UserIdToMongoObjectId" to both false and true, and neither helped.

{
  "data": {
    "register": null
  },
  "errors": [
    {
      "message": "ID cannot represent value: { _bsontype: \"ObjectID\", id: <Buffer 5b 9a 87 cb c0 98 4a 69 08 1c c6 d9> }",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "register"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: ID cannot represent value: { _bsontype: \"ObjectID\", id: <Buffer 5b 9a 87 cb c0 98 4a 69 08 1c c6 d9> }",
            "    at GraphQLScalarType.serializeID [as serialize] (\\node_modules\\graphql\\type\\scalars.js:212:9)",
            "    at completeLeafValue (\\node_modules\\graphql\\execution\\execute.js:641:37)",
            "    at completeValue (\\node_modules\\graphql\\execution\\execute.js:587:12)",
            "    at \\node_modules\\graphql\\execution\\execute.js:500:16",
            "    at <anonymous>",
            "    at process._tickCallback (internal/process/next_tick.js:188:7)"
          ]
        }
      }
    }
  ]
}
@ZachHaber
Copy link
Author

I just found out that this was caused by a breaking change in graphql 14.x. And they are looking to fixing it: graphql/graphql-js#1520. Sorry about that.

@williamli
Copy link
Contributor

williamli commented Sep 27, 2018

@ZachHaber any idea what I can do now to get accounts-js working without waiting for an update in graphql side?

@pradel
Copy link
Member

pradel commented Sep 27, 2018

@williamli you can use graphql 0.13.x

@williamli
Copy link
Contributor

@pradel does graphql 0.13.x work with Apollo server 2?

@pradel
Copy link
Member

pradel commented Sep 27, 2018

Yes it is :)

@TrillCyborg
Copy link

@williamli you can provide a type definition for id on your server that will send the String id to your client instead of the ObjectID id. It should look something like this...

const accountMutations = accountsGraphQL.resolvers.Mutation;
const registerMutation = accountMutations.register;

const resolvers = {
  Query: {
    ...,
  },
  Mutation: {
    ...,
    register: async (...args) => {
      const userId = await registerMutation(...args);
      return userId.toString();
    },
  },
  User: {
    id: ({  _id }) => typeof _id === 'string' ? _id : _id.toString();
  },
  LoginResult: {
    sessionId: ({ sessionId }) => sessionId.toString(),
  },
};

@williamli
Copy link
Contributor

@TrillCyborg so basically I need to wrap all calls that returns ID with a .toString()...

I think i will just switch to 0.13 for now until this issue is resolved in 14.x

@TrillCyborg
Copy link

@williamli yes, but what I have here is what I'm using. It covers all cases. There's only User.id and LoginResult.sessionId

@williamli
Copy link
Contributor

@TrillCyborg it's working fine, thanks

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

No branches or pull requests

4 participants