Use
camelCasefor all schema fields to keep your code base consistent
Also try to define your backend models/fields withcamelCase
type Task {
id: ID!
name: String!
complete: Boolean!
}Use capitalization for your
enumsto keep your teammates happy :)
enum Status {
DONE
IN_PROGRESS
REJECTED
}Use custom scalar types if you have some specific requirements for basic types. For example you can right your own
instead of usingStringin your schema.
Link: Official docs about custom types
// Schema code
const typeDefs = gql`
scalar Email
type User {
id: ID!
email: Email!
}
`;Define logic related to scalar type in your resolver.
const { GraphQLScalarType, Kind } = require('graphql');
const emailScalar = new GraphQLScalarType({
name: 'Email',
description: 'Email custom scalar type',
serialize(value) {
/// Logic For email validation
},
parseValue(value) {
/// Logic For email validation
},
parseLiteral(value) {
if (value.kind !== Kind.STRING) {
throw new GraphQLError(
`Email could be only string: ${value.kind}`,
);
}
/// Logic For email validation
},
});
const resolvers = {
Email: emailScalar
// ...other resolver definitions...
};
const server = new ApolloServer({
typeDefs,
resolvers
});In case if your schema should use few specific values in field you should define
enumfor that
instead of basic types.
scalar Email
enum Status {
APPROVED
IN_PROGRESS
REJECTED
}
type User {
id: ID!
status: Status
email: Email
}Lets use
sort,filter,paginationinputs accordingly.
enum UserSort {
ASC
DESC
}
input SortInput {
type: UserSort
}
input FilterInput {
search: String
byName: String
byRating: Integer
}
input PaginationInput {
limit: Integer!
offset: Integer!
}
type Query {
users(sort: SortInput, filter: FilterInput, pagination: PaginationInput)
}Use namespaces for you mutations for better logic structure.
// Create Namespace type for UserMutations
const UserMutations = new GraphQLObjectType({
name: 'UserMutations',
fields: () => {
follow: {
type: GraphQLBoolean,
resolve: () => { /* resolver code */ },
},
unfollow: {
type: GraphQLBoolean,
resolve: () => { /* resolver code */ },
},
},
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: () => {
users: {
type: UserMutations,
resolve: () => ({}),
}
},
});This code gives your ability to use your mutations like that:
mutation {
users {
follow(id: 15)
}
}