can query user

This commit is contained in:
Kalle (Sendou) 2020-10-13 23:26:43 +03:00
parent 6753b7e29c
commit 9403fb7bfe
9 changed files with 217 additions and 7 deletions

9
graphql/context.ts Normal file
View File

@ -0,0 +1,9 @@
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export interface Context {
prisma: PrismaClient;
}
export const createContext = (): Context => ({ prisma })

28
graphql/schema/index.ts Normal file
View File

@ -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",
},
],
},
});

28
graphql/schema/user.ts Normal file
View File

@ -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
}
})
})
}
})

118
nexus-typegen.ts Normal file
View File

@ -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<TypeName extends string> {
model: NexusPrisma<TypeName, 'model'>
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<TypeName extends string> {
}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginSchemaConfig {
}
}

15
pages/api/graphql.ts Normal file
View File

@ -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" });

View File

@ -1,4 +0,0 @@
export default (req, res) => {
res.statusCode = 200
res.json({ name: 'John Doe' })
}

View File

@ -1,5 +1,6 @@
datasource db {
provider = "postgresql"
// FIXME: should use same .env system as Next.JS
url = env("DATABASE_URL")
}

14
schema.graphql Normal file
View File

@ -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!
}

View File

@ -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": {