Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): generating schema from script #48

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"dependencies": {
"@entria/graphql-mongo-helpers": "^1.1.2",
"@entria/graphql-mongoose-loader": "^4.3.2",
"@koa/router": "10.0.0",
"@types/jest": "^27.5.1",
"babel-node": "^0.0.1-security",
"dataloader": "^1.4.0",
"get-graphql-schema": "^2.1.2",
"esbuild": "^0.12.5",
"get-graphql-schema": "^2.1.2",
"graphql": "^15.5.0",
"graphql-relay": "^0.10.0",
"graphql-relay": "^0.9.0",
"koa": "^2.13.1",
"koa-graphql": "^0.8.0",
"mongoose": "^5.12.12",
Expand Down Expand Up @@ -56,7 +58,7 @@
"scripts": {
"start": "webpack --watch --progress --config webpack.js",
"jest": "jest",
"build": "esbuild app.jsx --bundle --outfile = out.js",
"schema": "babel-node --extensions \".es6,.js,.es,.jsx,.mjs,.ts\" ./scripts/updateSchema.ts"
"build": "babel-node --extensions \".es6,.js,.es,.jsx,.mjs,.ts,.tsx\"",
"schema": "yarn build webpackx.ts ./scripts/updateSchema.ts"
}
}
102 changes: 102 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""The root of all... queries"""
type Query {
"""Fetches an object given its ID"""
node(
"""The ID of an object"""
id: ID!
): Node

"""Fetches objects given their IDs"""
nodes(
"""The IDs of objects"""
ids: [ID!]!
): [Node]!
events(after: String, first: Int, before: String, last: Int): EventConnection!
}

"""An object with an ID"""
interface Node {
"""The id of the object."""
id: ID!
}

"""A connection to a list of items."""
type EventConnection {
"""Number of items in this connection"""
count: Int

"""
A count of the total number of objects in this connection, ignoring pagination.
This allows a client to fetch the first five objects by passing "5" as the
argument to "first", then fetch the total count so it could display "5 of 83",
for example.
"""
totalCount: Int

"""Offset from start"""
startCursorOffset: Int!

"""Offset till end"""
endCursorOffset: Int!

"""Information to aid in pagination."""
pageInfo: PageInfoExtended!

"""A list of edges."""
edges: [EventEdge]!
}

"""Information about pagination in a connection."""
type PageInfoExtended {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!

"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!

"""When paginating backwards, the cursor to continue."""
startCursor: String

"""When paginating forwards, the cursor to continue."""
endCursor: String
}

"""An edge in a connection."""
type EventEdge {
"""The item at the end of the edge"""
node: Event

"""A cursor for use in pagination"""
cursor: String!
}

"""event data"""
type Event implements Node {
"""The ID of an object"""
id: ID!
name: String
start: String
end: String
allDay: Boolean
}

"""Root of ... mutations"""
type Mutation {
CreateEvent(input: Create eventInput!): Create eventPayload
}

type Create eventPayload {
event: Event

"""Default error field resolver."""
error: String

"""Default success field resolver."""
success: String
clientMutationId: String
}

input Create eventInput {
event: ID!
clientMutationId: String
}
143 changes: 143 additions & 0 deletions src/graphql/connectionDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {
GraphQLBoolean,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldResolver,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
Thunk,
} from "graphql";

export const forwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
after: {
type: GraphQLString,
},
first: {
type: GraphQLInt,
},
};

export const backwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
before: {
type: GraphQLString,
},
last: {
type: GraphQLInt,
},
};

export const connectionArgs: GraphQLFieldConfigArgumentMap = {
...forwardConnectionArgs,
...backwardConnectionArgs,
};

type ConnectionConfig = {
name?: string | null;
nodeType: GraphQLObjectType;
resolveNode?: GraphQLFieldResolver<any, any> | null;
resolveCursor?: GraphQLFieldResolver<any, any> | null;
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>> | null;
connectionFields?: Thunk<GraphQLFieldConfigMap<any, any>> | null;
};

export type GraphQLConnectionDefinitions = {
edgeType: GraphQLObjectType;
connectionType: GraphQLObjectType;
};

const pageInfoType = new GraphQLObjectType({
name: "PageInfoExtended",
description: "Information about pagination in a connection.",
fields: () => ({
hasNextPage: {
type: GraphQLNonNull(GraphQLBoolean),
description: "When paginating forwards, are there more items?",
},
hasPreviousPage: {
type: GraphQLNonNull(GraphQLBoolean),
description: "When paginating backwards, are there more items?",
},
startCursor: {
type: GraphQLString,
description: "When paginating backwards, the cursor to continue.",
},
endCursor: {
type: GraphQLString,
description: "When paginating forwards, the cursor to continue.",
},
}),
});

function resolveMaybeThunk<T>(thingOrThunk: Thunk<T>): T {
return typeof thingOrThunk === "function"
? (thingOrThunk as () => T)()
: thingOrThunk;
}

export function connectionDefinitions(
config: ConnectionConfig
): GraphQLConnectionDefinitions {
const { nodeType, resolveCursor, resolveNode } = config;
const name = config.name || nodeType.name;
const edgeFields = config.edgeFields || {};
const connectionFields = config.connectionFields || {};

const edgeType = new GraphQLObjectType({
name: `${name}Edge`,
description: "An edge in a connection.",
fields: () => ({
node: {
type: nodeType,
resolve: resolveNode,
description: "The item at the end of the edge",
},
cursor: {
type: GraphQLNonNull(GraphQLString),
resolve: resolveCursor,
description: "A cursor for use in pagination",
},
...(resolveMaybeThunk(edgeFields) as any),
}),
});

const connectionType = new GraphQLObjectType({
name: `${name}Connection`,
description: "A connection to a list of items.",
fields: () => ({
count: {
type: GraphQLInt,
description: "Number of items in this connection",
},
totalCount: {
type: GraphQLInt,
resolve: (connection) => connection.count,
description: `A count of the total number of objects in this connection, ignoring pagination.
This allows a client to fetch the first five objects by passing "5" as the
argument to "first", then fetch the total count so it could display "5 of 83",
for example.`,
},
startCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: "Offset from start",
},
endCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: "Offset till end",
},
pageInfo: {
type: GraphQLNonNull(pageInfoType),
description: "Information to aid in pagination.",
},
edges: {
type: GraphQLNonNull(GraphQLList(edgeType)),
description: "A list of edges.",
},
...(resolveMaybeThunk(connectionFields) as any),
}),
});

return { edgeType, connectionType };
}
2 changes: 2 additions & 0 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { registerLoader, getDataloaders } from "./loaderRegister";
export { connectionArgs, connectionDefinitions } from "./connectionDefinitions";
2 changes: 1 addition & 1 deletion src/modules/event/EventType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GraphQLBoolean, GraphQLObjectType, GraphQLString } from "graphql";

import { globalIdField } from "graphql-relay";

import { connectionDefinitions } from "@entria/graphql-mongo-helpers";
import { connectionDefinitions } from "../../graphql";

import { load } from "./EventLoader";

Expand Down
2 changes: 1 addition & 1 deletion src/schema/QueryType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GraphQLObjectType, GraphQLNonNull } from 'graphql';

import { connectionArgs } from "@entria/graphql-mongo-helpers";
import { connectionArgs } from "../graphql/connectionDefinitions";

import { nodesField, nodeField } from '../modules/node/typeRegister';
import * as EventLoader from '../modules/event/EventLoader';
Expand Down
64 changes: 64 additions & 0 deletions webpack/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const path = require("path");

const nodeExternals = require("webpack-node-externals");

const cwd = process.cwd();

export const outputPath = path.join(cwd, ".webpack");
export const outputFilename = "bundle.js";

export default {
context: cwd,
mode: "development",
devtool: false,
resolve: {
extensions: [".ts", ".tsx", ".js", ".json", ".mjs"],
// alias: {
// mongoose: 'mongoosev5',
// mongodb: 'mongodbv3',
// },
},
output: {
libraryTarget: "commonjs2",
path: outputPath,
filename: outputFilename,
},
target: "node",
externals: [
nodeExternals({
allowlist: [/@feedback/, /@openpix/],
}),
nodeExternals({
modulesDir: path.resolve(__dirname, "../node_modules"),
allowlist: [/@feedback/, /@openpix/],
}),
],
module: {
rules: [
{
test: /\.mjs$/,
type: "javascript/auto",
},
{
test: /\.(js|jsx|ts|tsx)?$/,
use: {
loader: "babel-loader?cacheDirectory",
},
exclude: [
/node_modules/,
path.resolve(__dirname, ".serverless"),
path.resolve(__dirname, ".webpack"),
],
},
{
test: /\.(pem|p12)?$/,
type: "asset/source",
},
],
},
plugins: [],
node: {
__dirname: false,
__filename: false,
},
};
Loading