Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { objectType, queryType, mutationType } from 'nexus'
export const Query = queryType({
definition(t) {
// ASSERT findOne & findMany
t.crud.user()
t.crud.users()
},
})
export const Mutation = mutationType({
definition: t => {
t.crud.createOnePost()
t.crud.updateManyPost()
},
})
export const User = objectType({
name: 'User',
import { objectType, queryType, mutationType } from 'nexus'
export const Query = queryType({
definition(t) {
// ASSERT findOne & findMany
t.crud.user()
t.crud.users()
},
})
export const Mutation = mutationType({
definition: t => {
t.crud.createOnePost()
t.crud.updateManyPost()
},
})
export const User = objectType({
name: 'User',
definition(t) {
// ASSERT CUID maps to GQL ID
t.model.id()
t.model.firstName()
// ASSERT pagination automatically enabled
// ASSERT exposes filtering if true
// ASSERT exposes ordering if true
t.model.posts({ filtering: true, ordering: true })
import { queryType, stringArg, makeSchema } from 'nexus'
import path from 'path'
const PORT = 4000
const GENERATED_OUTPUT_PATH = `${path.resolve('.')}/__generated/`
const Query = queryType({
definition(t) {
t.string('hello', {
args: { name: stringArg({ nullable: true }) },
resolve: (parent, { name }) => `Hello ${name || 'world'}`,
})
},
})
const schema = makeSchema({
types: [Query],
outputs: {
schema: `${GENERATED_OUTPUT_PATH}schema.graphql`,
typegen: `${GENERATED_OUTPUT_PATH}typings.ts`,
},
})
const server = new ApolloServer({
schema,
})
server.listen({ port: PORT }, () => console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`))
// @ts-ignore
export default server.httpServer
// requiredViewer throws, so the app can redirect.
export const requiredViewer = queryField('requiredViewer', {
type: User,
resolve: (_root, _args, context) => context.models.user.requiredViewer(),
});
export const SignInInput = inputObjectType({
name: 'SignInInput',
definition(t) {
t.string('email');
t.string('password');
t.boolean('createAccount');
},
});
export const SignInErrors = objectType({
nonNullDefaults: { output: false },
name: 'SignInErrors',
definition(t) {
t.field('email', { type: 'EmailError' });
t.field('password', { type: 'PasswordError' });
},
});
export const SignInPayload = objectType({
nonNullDefaults: { output: false },
name: 'SignInPayload',
definition(t) {
t.field('errors', { type: SignInErrors });
t.string('token');
},
});
return ctx.photon.users.findMany();
}
});
t.list.field('feedDecks', {
type: 'Deck',
resolve: (_parent, _args, ctx) => {
return ctx.photon.decks.findMany();
}
});
t.list.field('deckConnection', {
type: 'Deck',
args: {
first: intArg(),
last: stringArg()
},
resolve: (_parent, args, ctx) => {
const { first, last } = args;
return ctx.photon.decks.findMany({
first: first,
after: last
});
}
});
// t.list.field('feed', {
// type: 'Post',
// resolve: (_parent, _args, ctx) => {
// return ctx.photon.posts.findMany({
// where: { published: true },
// })
} as any,
resolve: (parent, { name, email }, ctx) => {
return ctx.photon.users.create({
data: {
name,
email,
},
})
},
})
t.field('createDraft', {
type: 'Post',
args: {
title: stringArg(),
content: stringArg({ nullable: true }),
authorEmail: stringArg(),
} as any,
resolve: (parent, { title, content, authorEmail }, ctx) => {
return ctx.photon.posts.create({
data: {
title,
content,
published: false,
// author: {
// connect: { email: authorEmail },
// },
},
})
},
})
import { getUser } from './getUser';
import { createModels } from './models';
const { PRISMA_ENDPOINT, PRISMA_SECRET, API_SECRET } = process.env;
if (!PRISMA_ENDPOINT || !PRISMA_SECRET || !API_SECRET)
throw Error(`Did you run 'yarn env dev'?`);
// Nullability
// https://blog.apollographql.com/designing-graphql-mutations-e09de826ed97
// https://blog.apollographql.com/using-nullability-in-graphql-2254f84c4ed7
// Remember
// For input arguments and fields, adding non-null is a breaking change.
// For output fields, removing non-null from a field is a breaking change.
// By id queries like web(id: ID!): Web! returns always non nullable value,
// because errors are passed as permissions exists or isWebCreatorOrAdmin etc.
const schema = makeSchema({
types: schemaTypes,
// All inputs are non nullable.
// Remember, after release, every new input must be nullable.
// Note payloads and errors needs nonNullDefaults: { output: false }.
nonNullDefaults: { input: true },
outputs: {
schema: path.join(__dirname, './schema.graphql'),
typegen: path.join(__dirname, './typegen.ts'),
},
typegenAutoConfig: {
// debug: true,
sources: [{ source: path.join(__dirname, './types.ts'), alias: 'types' }],
contextType: 'types.Context',
},
// backingTypeMap: {
// EmailAddress: 'string',
import { enumType } from 'nexus';
// https://itnext.io/the-definitive-guide-to-handling-graphql-errors-e0c58b52b5e1
export const EmailError = enumType({
name: 'EmailError',
members: ['REQUIRED', 'EMAIL', 'ALREADY_EXISTS', 'NOT_EXISTS'],
});
export const URLError = enumType({
name: 'URLError',
members: ['REQUIRED', 'URL'],
});
export const PasswordError = enumType({
name: 'PasswordError',
members: ['REQUIRED', 'MIN_5_CHARS', 'MAX_1024_CHARS', 'WRONG_PASSWORD'],
});
export const Max140CharsError = enumType({
name: 'Max140CharsError',
members: ['REQUIRED', 'MAX_140_CHARS'],
});
publishInputObjectType(inputType: DMMF.Data.InputType) {
this.markTypeAsPublished(inputType.name)
return Nexus.inputObjectType({
name: inputType.name,
definition: t => {
inputType.fields
.map(field => ({
...field,
inputType: {
...field.inputType,
type: this.isPublished(field.inputType.type)
? // Simply reference the field input type if it's already been visited, otherwise create it
field.inputType.type
: this.inputType({
arg: field,
type: this.getTypeFromArg(field),
}),
},
}))
});
// https://medium.com/workflowgen/graphql-schema-design-the-viewer-field-aeabfacffe72
export const viewer = queryField('viewer', {
type: User,
nullable: true,
resolve: (_root, _args, context) => context.models.user.viewer(),
});
// requiredViewer throws, so the app can redirect.
export const requiredViewer = queryField('requiredViewer', {
type: User,
resolve: (_root, _args, context) => context.models.user.requiredViewer(),
});
export const SignInInput = inputObjectType({
name: 'SignInInput',
definition(t) {
t.string('email');
t.string('password');
t.boolean('createAccount');
},
});
export const SignInErrors = objectType({
nonNullDefaults: { output: false },
name: 'SignInErrors',
definition(t) {
t.field('email', { type: 'EmailError' });
t.field('password', { type: 'PasswordError' });
},
});