I'm using graphql + mysql + react-apollo and here's one of the graphql type for User
table:
type User {
id: ID!
name: String!
}
My issue with ID scalar type
in graphql
is that it is returned as a string when primary keys are int
in mysql and it has created some type conflicts on the frontend with typescript.
Can I just simply not use ID scalar type at all, given that I have already set a unique identifier with dataIdFromObject
for each object in Apollo Client:
import {InMemoryCache} from 'apollo-cache-inmemory';
const apolloMemoryCache = new InMemoryCache(
{
dataIdFromObject: ({id,__typename}) => {
return __typename+id
}
}
);
const client = new ApolloClient({
link: ApolloLink.from([authLink, httpLink]),
cache: apolloMemoryCache,
});
Would you keep the ID type or just ditch it?
You asked
would you keep the ID type or just ditch it
I generally recommend keeping the ID type, and NOT exposing to the client the integer that you are using. If this is a new dataset, you're probably even going to be better off using uuids as the PK from the offset. It's going to be "safer" and "more secure", as you're less likely to accidentally give someone access to someone else's stuff.
Either way, I would recommend making the IDs "opaque", as per the Relay spec, so that you have the ability to change them later, if you change your datastore, without affecting your clients. Apps often do their own type-checking, so you want to make sure your stuff isn't going to ever change in that regard whenever possible.
You should define a custom scalar for your resolver.
In your resolver you should add one for ID
where you expect an int or you can do your conversion between int and string in your resolver.
import { GraphQLScalarType } from 'graphql';
const resolverMap = {
ID: new GraphQLScalarType({
name: 'ID',
description: 'Numeric custom scalar type',
parseValue(value) {
let result;
// Implement your own behavior here by setting the 'result' variable
return result;
},
serialize(value) {
let result;
// Implement your own behavior here by setting the 'result' variable
return result;
},
parseLiteral(ast) {
switch (ast.kind) {
// Implement your own behavior here by returning what suits your needs
// depending on ast.kind
}
}
}),
};
https://github.com/apollographql/graphql-tools/blob/master/docs/source/scalars.md#custom-graphqlscalartype-instance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With