-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Query/Mutation receives args object with a null prototype #3149
Comments
Hey @lookfirst, thanks for reporting an issue. If I understood correctly, this problem is experienced by non-AS users as well? This led me to believe it may be one of the underlying libraries instead (graphql-tools or graphql-js, most likely). I put together a minimal reproduction to confirm my suspicion and found it to be true: I was also surprised to find that the args for a query were also an object with a null prototype:
which is inconsistent with what you said unless I misunderstood:
I confirmed the behavior is consistent in both my gist and an ApolloServer-fied version of my gist. I'm going to close this issue, since it's not ApolloServer's doing, but an underlying library. Please feel free to reference this issue elsewhere if you choose to confirm where this is specifically coming from. Related: I'm unsure why this is an issue for a consumer. Is it problematic for logging purposes, or is this actually having an impact on the expected inputs? You should be able to access everything on the |
Interesting. Thanks for reproducing outside of Apollo. It will help me dig deeper. I did not notice this on the query because I was only defining Foo(id: ID!) and not a full object. Passing these objects into firebase/firestore for mutation causes failure because |
Related: graphql/express-graphql#177 |
Google fixed this in firestore! googleapis/nodejs-firestore#736 |
@lookfirst Hey, nice work - and thanks for the update. Glad it all worked out 😄 |
You can just distructure an object with [Object: null prototype] |
any solution for this? |
|
I made a simple library to fix this. The library is faster than going to json and back because it works in place. Link: https://www.npmjs.com/package/normalize-object-inheritance |
FWIW, I'm pretty sure this is a feature. It means that you can have args with names like |
graphql should return plain data that can be used safely with other programs. For example, nodejs' native assert will fail when comparing graphql's import assert from 'node:assert/strict';
import gqlreq from './gqlreq.js';
const gqlres = await gqlreq();
console.log(gqlres);
// [Object: null prototype] {
// user: [Object: null prototype] {
// id: 'ywj--Y3SxiXk4TEa04Wz5'
// }
// }
assert.deepEqual(gqlres, {
user: {
id: 'ywj--Y3SxiXk4TEa04Wz5'
}
}); // ERR_ASSERTION, Expected values to be strictly deep-equal |
FWIW this choice is made inside It's discussed at length in this PR: graphql/graphql-js#504 (comment) As I commented there a few months ago, the biggest strangeness is that The issue with fields that have names like |
Acknowledged. Here's little work-around that deep-recurses the graphql object to return a normal object that can be used with other tools like node's assert. If your objects aren't too deep, this might be OK for unit-tests const ungqlval = (value, type = typeof value, f = null) => {
if (value === f || /string|number|boolean/.test(type))
f = value;
else if (Array.isArray(value) || type === 'object')
f = ungql(value);
return f;
};
const ungql = data => Object.keys(data).reduce((prev, k) => {
prev[k] = ungqlval(data[k]);
return prev;
}, Array.isArray(data) ? [] : {}); |
This bug report should include:
Version: latest apollo-server-core (2.8.1)
Related SO
https://stackoverflow.com/questions/53983315/is-there-a-way-to-get-rid-of-object-null-prototype-in-graphql
Actual behavior
This has been discussed on SO, but the solution is less than ideal. Serializing things through json is silly overhead. If anything it is confusing and undefined/undocumented behavior. I'm filing this as a bug since a mutation should get an object passed in with a prototype. A query receives an
args
object with a prototype. This behavior should at least be made consistent and ideally an object with a prototype.Example code:
Output:
Expected behavior
Current workaround
The text was updated successfully, but these errors were encountered: