From 9403fb7bfef1d9c566cace0792bdfd46906fabf4 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Tue, 13 Oct 2020 23:26:43 +0300 Subject: [PATCH] can query user --- graphql/context.ts | 9 +++ graphql/schema/index.ts | 28 ++++++++++ graphql/schema/user.ts | 28 ++++++++++ nexus-typegen.ts | 118 ++++++++++++++++++++++++++++++++++++++++ pages/api/graphql.ts | 15 +++++ pages/api/hello.tsx | 4 -- prisma/schema.prisma | 1 + schema.graphql | 14 +++++ tsconfig.json | 7 ++- 9 files changed, 217 insertions(+), 7 deletions(-) create mode 100644 graphql/context.ts create mode 100644 graphql/schema/index.ts create mode 100644 graphql/schema/user.ts create mode 100644 nexus-typegen.ts create mode 100644 pages/api/graphql.ts delete mode 100644 pages/api/hello.tsx create mode 100644 schema.graphql diff --git a/graphql/context.ts b/graphql/context.ts new file mode 100644 index 000000000..cfdad5599 --- /dev/null +++ b/graphql/context.ts @@ -0,0 +1,9 @@ +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +export interface Context { + prisma: PrismaClient; +} + +export const createContext = (): Context => ({ prisma }) \ No newline at end of file diff --git a/graphql/schema/index.ts b/graphql/schema/index.ts new file mode 100644 index 000000000..fbff1d8c4 --- /dev/null +++ b/graphql/schema/index.ts @@ -0,0 +1,28 @@ +import { makeSchema } from "@nexus/schema"; +import * as userTypes from "graphql/schema/user"; +import { nexusSchemaPrisma } from "nexus-plugin-prisma/schema"; +import path from "path"; + + +export const schema = makeSchema({ + types: [userTypes], + plugins: [nexusSchemaPrisma()], + outputs: { + // FIXME: should be in graphql/generated instead root + schema: path.join(process.cwd(), "schema.graphql"), + typegen: path.join(process.cwd(), "nexus-typegen.ts"), + }, + typegenAutoConfig: { + contextType: "Context.Context", + sources: [ + { + source: "@prisma/client", + alias: "prisma", + }, + { + source: require.resolve("graphql/context"), + alias: "Context", + }, + ], + }, +}); \ No newline at end of file diff --git a/graphql/schema/user.ts b/graphql/schema/user.ts new file mode 100644 index 000000000..99524c710 --- /dev/null +++ b/graphql/schema/user.ts @@ -0,0 +1,28 @@ +import { objectType, queryType, stringArg } from "@nexus/schema"; + +export const User = objectType({ + name: "User", + definition(t) { + t.model.id(); + t.model.username(); + t.model.discriminator(); + t.string("fullUserName", {resolve: (root, asd) => `${root.username}`}) + }, +}); + +export const Query = queryType({ + definition(t) { + t.field("getUserByIdentifier", { + type: User, + nullable: true, + args: { + identifier: stringArg({required: true}) + }, + resolve: (_root, {identifier}, ctx) => ctx.prisma.user.findOne({ + where: { + discordId: identifier + } + }) + }) + } +}) \ No newline at end of file diff --git a/nexus-typegen.ts b/nexus-typegen.ts new file mode 100644 index 000000000..df9064efe --- /dev/null +++ b/nexus-typegen.ts @@ -0,0 +1,118 @@ +/** + * This file was generated by Nexus Schema + * Do not make changes to this file directly + */ + +import * as Context from "./graphql/context" + + + +declare global { + interface NexusGenCustomOutputProperties { + model: NexusPrisma + crud: any + } +} + +declare global { + interface NexusGen extends NexusGenTypes {} +} + +export interface NexusGenInputs { +} + +export interface NexusGenEnums { +} + +export interface NexusGenScalars { + String: string + Int: number + Float: number + Boolean: boolean + ID: string +} + +export interface NexusGenRootTypes { + Query: {}; + User: { // root type + discriminator: string; // String! + id: number; // Int! + username: string; // String! + } +} + +export interface NexusGenAllTypes extends NexusGenRootTypes { + String: NexusGenScalars['String']; + Int: NexusGenScalars['Int']; + Float: NexusGenScalars['Float']; + Boolean: NexusGenScalars['Boolean']; + ID: NexusGenScalars['ID']; +} + +export interface NexusGenFieldTypes { + Query: { // field return type + getUserByIdentifier: NexusGenRootTypes['User'] | null; // User + } + User: { // field return type + discriminator: string; // String! + fullUserName: string | null; // String + id: number; // Int! + username: string; // String! + } +} + +export interface NexusGenArgTypes { + Query: { + getUserByIdentifier: { // args + identifier: string; // String! + } + } +} + +export interface NexusGenAbstractResolveReturnTypes { +} + +export interface NexusGenInheritedFields {} + +export type NexusGenObjectNames = "Query" | "User"; + +export type NexusGenInputNames = never; + +export type NexusGenEnumNames = never; + +export type NexusGenInterfaceNames = never; + +export type NexusGenScalarNames = "Boolean" | "Float" | "ID" | "Int" | "String"; + +export type NexusGenUnionNames = never; + +export interface NexusGenTypes { + context: Context.Context; + inputTypes: NexusGenInputs; + rootTypes: NexusGenRootTypes; + argTypes: NexusGenArgTypes; + fieldTypes: NexusGenFieldTypes; + allTypes: NexusGenAllTypes; + inheritedFields: NexusGenInheritedFields; + objectNames: NexusGenObjectNames; + inputNames: NexusGenInputNames; + enumNames: NexusGenEnumNames; + interfaceNames: NexusGenInterfaceNames; + scalarNames: NexusGenScalarNames; + unionNames: NexusGenUnionNames; + allInputTypes: NexusGenTypes['inputNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['scalarNames']; + allOutputTypes: NexusGenTypes['objectNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['unionNames'] | NexusGenTypes['interfaceNames'] | NexusGenTypes['scalarNames']; + allNamedTypes: NexusGenTypes['allInputTypes'] | NexusGenTypes['allOutputTypes'] + abstractTypes: NexusGenTypes['interfaceNames'] | NexusGenTypes['unionNames']; + abstractResolveReturn: NexusGenAbstractResolveReturnTypes; +} + + +declare global { + interface NexusGenPluginTypeConfig { + } + interface NexusGenPluginFieldConfig { + } + interface NexusGenPluginSchemaConfig { + } +} \ No newline at end of file diff --git a/pages/api/graphql.ts b/pages/api/graphql.ts new file mode 100644 index 000000000..7b596ced5 --- /dev/null +++ b/pages/api/graphql.ts @@ -0,0 +1,15 @@ +import { ApolloServer } from "apollo-server-micro"; +import { createContext } from "graphql/context"; +import { schema } from "graphql/schema"; + +export const config = { + api: { + bodyParser: false, + }, +}; + +export default new ApolloServer({ + schema, + context: createContext, + tracing: process.env.NODE_ENV === "development", +}).createHandler({ path: "/api/graphql" }); \ No newline at end of file diff --git a/pages/api/hello.tsx b/pages/api/hello.tsx deleted file mode 100644 index adf9d6422..000000000 --- a/pages/api/hello.tsx +++ /dev/null @@ -1,4 +0,0 @@ -export default (req, res) => { - res.statusCode = 200 - res.json({ name: 'John Doe' }) -} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 17433bcea..ff2c6458f 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -1,5 +1,6 @@ datasource db { provider = "postgresql" + // FIXME: should use same .env system as Next.JS url = env("DATABASE_URL") } diff --git a/schema.graphql b/schema.graphql new file mode 100644 index 000000000..986f81be9 --- /dev/null +++ b/schema.graphql @@ -0,0 +1,14 @@ +### This file was generated by Nexus Schema +### Do not make changes to this file directly + + +type Query { + getUserByIdentifier(identifier: String!): User +} + +type User { + discriminator: String! + fullUserName: String + id: Int! + username: String! +} diff --git a/tsconfig.json b/tsconfig.json index 6fc9426f5..10ef974e9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,9 @@ "dom.iterable", "esnext" ], - "allowJs": true, + "allowJs": false, "skipLibCheck": true, - "strict": false, + "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, @@ -16,7 +16,8 @@ "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, - "jsx": "preserve" + "jsx": "preserve", + "baseUrl": "." }, "ts-node": { "compilerOptions": {