@kubun/graphql 0.17.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/context.d.ts CHANGED
@@ -3,7 +3,7 @@ import type { AggregateHaving, AggregateSpec, AggregateValues, DocumentData, Doc
3
3
  import type { SearchDocumentResult, SearchDocumentsParams, StoredAccessDefaultRow, ViewerReadAccess } from '@kubun/store-graph';
4
4
  import type { Tracer } from '@sozai/otel';
5
5
  import type { GraphQLResolveInfo } from 'graphql';
6
- import type { Connection, Edge } from 'graphql-relay';
6
+ import type { Connection, Edge } from './relay.js';
7
7
  import type { AllConnectionArguments, ModelAccessDefaults, UpdateMutationInput } from './types.js';
8
8
  /**
9
9
  * Event data for document save notifications used by subscriptions.
@@ -90,7 +90,7 @@ export type AggregateScope = {
90
90
  };
91
91
  /**
92
92
  * Connection value returned by {@link QueryContext.resolveConnection}. Extends
93
- * the graphql-relay connection (edges/pageInfo are untouched) with the query
93
+ * the Relay connection (edges/pageInfo are untouched) with the query
94
94
  * scope so the `aggregates` field can resolve consistently with the page.
95
95
  */
96
96
  export type ScopedConnection = Connection<DocumentNode> & {
package/lib/relay.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ import { type GraphQLFieldConfig, type GraphQLFieldConfigArgumentMap, type GraphQLFieldExtensions, type GraphQLFieldResolver, type GraphQLInputFieldConfig, GraphQLInterfaceType, type GraphQLNamedOutputType, GraphQLNonNull, GraphQLObjectType, type GraphQLResolveInfo, type GraphQLTypeResolver, type ThunkObjMap } from 'graphql';
2
+ /**
3
+ * Internal reimplementation of the small slice of `graphql-relay` this package
4
+ * uses (connection args/types, `Node` interface, and Relay-style mutations).
5
+ * The emitted GraphQL types are byte-identical to `graphql-relay@0.10`, which
6
+ * the schema snapshot tests verify.
7
+ */
8
+ /** A type alias for cursors in this implementation. */
9
+ export type ConnectionCursor = string;
10
+ /** A type describing the arguments a connection field receives in GraphQL. */
11
+ export type ConnectionArguments = {
12
+ before?: ConnectionCursor | null;
13
+ after?: ConnectionCursor | null;
14
+ first?: number | null;
15
+ last?: number | null;
16
+ };
17
+ /** A type designed to be exposed as a `Connection` over GraphQL. */
18
+ export type Connection<T> = {
19
+ edges: Array<Edge<T>>;
20
+ pageInfo: PageInfo;
21
+ };
22
+ /** A type designed to be exposed as an `Edge` over GraphQL. */
23
+ export type Edge<T> = {
24
+ node: T;
25
+ cursor: ConnectionCursor;
26
+ };
27
+ /** A type designed to be exposed as `PageInfo` over GraphQL. */
28
+ export type PageInfo = {
29
+ startCursor: ConnectionCursor | null;
30
+ endCursor: ConnectionCursor | null;
31
+ hasPreviousPage: boolean;
32
+ hasNextPage: boolean;
33
+ };
34
+ /**
35
+ * Args to include on a field whose return type is a connection type with
36
+ * forward pagination.
37
+ */
38
+ export declare const forwardConnectionArgs: GraphQLFieldConfigArgumentMap;
39
+ /**
40
+ * Args to include on a field whose return type is a connection type with
41
+ * backward pagination.
42
+ */
43
+ export declare const backwardConnectionArgs: GraphQLFieldConfigArgumentMap;
44
+ /**
45
+ * Args to include on a field whose return type is a connection type with
46
+ * bidirectional pagination.
47
+ */
48
+ export declare const connectionArgs: GraphQLFieldConfigArgumentMap;
49
+ export type ConnectionConfig = {
50
+ name?: string;
51
+ nodeType: GraphQLNamedOutputType | GraphQLNonNull<GraphQLNamedOutputType>;
52
+ resolveNode?: GraphQLFieldResolver<any, any>;
53
+ resolveCursor?: GraphQLFieldResolver<any, any>;
54
+ edgeFields?: ThunkObjMap<GraphQLFieldConfig<any, any>>;
55
+ connectionFields?: ThunkObjMap<GraphQLFieldConfig<any, any>>;
56
+ };
57
+ export type GraphQLConnectionDefinitions = {
58
+ edgeType: GraphQLObjectType;
59
+ connectionType: GraphQLObjectType;
60
+ };
61
+ /**
62
+ * Returns a GraphQLObjectType for a connection with the given name, and whose
63
+ * nodes are of the specified type.
64
+ */
65
+ export declare function connectionDefinitions(config: ConnectionConfig): GraphQLConnectionDefinitions;
66
+ export type GraphQLNodeDefinitions<TContext> = {
67
+ nodeInterface: GraphQLInterfaceType;
68
+ nodeField: GraphQLFieldConfig<unknown, TContext>;
69
+ nodesField: GraphQLFieldConfig<unknown, TContext>;
70
+ };
71
+ /**
72
+ * Given a function to map from an ID to an underlying object, and a function to
73
+ * map from an underlying object to its concrete GraphQLObjectType, constructs a
74
+ * `Node` interface plus `node`/`nodes` root field configs.
75
+ */
76
+ export declare function nodeDefinitions<TContext>(fetchById: (id: string, context: TContext, info: GraphQLResolveInfo) => unknown, typeResolver?: GraphQLTypeResolver<unknown, TContext>): GraphQLNodeDefinitions<TContext>;
77
+ type MutationFn<TInput, TOutput, TContext> = (object: TInput, ctx: TContext, info: GraphQLResolveInfo) => TOutput;
78
+ export type MutationConfig<TInput = any, TOutput = unknown, TContext = unknown> = {
79
+ name: string;
80
+ description?: string;
81
+ deprecationReason?: string;
82
+ extensions?: GraphQLFieldExtensions<unknown, unknown>;
83
+ inputFields: ThunkObjMap<GraphQLInputFieldConfig>;
84
+ outputFields: ThunkObjMap<GraphQLFieldConfig<TOutput, TContext>>;
85
+ mutateAndGetPayload: MutationFn<TInput, Promise<TOutput> | TOutput, TContext>;
86
+ };
87
+ /**
88
+ * Returns a GraphQLFieldConfig for the mutation described by the provided
89
+ * config, wiring the Relay `clientMutationId` round-trip through a generated
90
+ * `${name}Input`/`${name}Payload` pair.
91
+ */
92
+ export declare function mutationWithClientMutationId<TInput = any, TOutput = unknown, TContext = unknown>(config: MutationConfig<TInput, TOutput, TContext>): GraphQLFieldConfig<unknown, TContext>;
93
+ export {};
package/lib/relay.js ADDED
@@ -0,0 +1,197 @@
1
+ import { GraphQLBoolean, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString, getNamedType, resolveObjMapThunk } from 'graphql';
2
+ /**
3
+ * Args to include on a field whose return type is a connection type with
4
+ * forward pagination.
5
+ */ export const forwardConnectionArgs = Object.freeze({
6
+ after: {
7
+ type: GraphQLString,
8
+ description: 'Returns the items in the list that come after the specified cursor.'
9
+ },
10
+ first: {
11
+ type: GraphQLInt,
12
+ description: 'Returns the first n items from the list.'
13
+ }
14
+ });
15
+ /**
16
+ * Args to include on a field whose return type is a connection type with
17
+ * backward pagination.
18
+ */ export const backwardConnectionArgs = Object.freeze({
19
+ before: {
20
+ type: GraphQLString,
21
+ description: 'Returns the items in the list that come before the specified cursor.'
22
+ },
23
+ last: {
24
+ type: GraphQLInt,
25
+ description: 'Returns the last n items from the list.'
26
+ }
27
+ });
28
+ /**
29
+ * Args to include on a field whose return type is a connection type with
30
+ * bidirectional pagination.
31
+ */ export const connectionArgs = {
32
+ ...forwardConnectionArgs,
33
+ ...backwardConnectionArgs
34
+ };
35
+ /** The common page info type shared by every connection. */ const pageInfoType = new GraphQLObjectType({
36
+ name: 'PageInfo',
37
+ description: 'Information about pagination in a connection.',
38
+ fields: ()=>({
39
+ hasNextPage: {
40
+ type: new GraphQLNonNull(GraphQLBoolean),
41
+ description: 'When paginating forwards, are there more items?'
42
+ },
43
+ hasPreviousPage: {
44
+ type: new GraphQLNonNull(GraphQLBoolean),
45
+ description: 'When paginating backwards, are there more items?'
46
+ },
47
+ startCursor: {
48
+ type: GraphQLString,
49
+ description: 'When paginating backwards, the cursor to continue.'
50
+ },
51
+ endCursor: {
52
+ type: GraphQLString,
53
+ description: 'When paginating forwards, the cursor to continue.'
54
+ }
55
+ })
56
+ });
57
+ /**
58
+ * Returns a GraphQLObjectType for a connection with the given name, and whose
59
+ * nodes are of the specified type.
60
+ */ export function connectionDefinitions(config) {
61
+ const { nodeType } = config;
62
+ const name = config.name ?? getNamedType(nodeType).name;
63
+ const edgeType = new GraphQLObjectType({
64
+ name: `${name}Edge`,
65
+ description: 'An edge in a connection.',
66
+ fields: ()=>({
67
+ node: {
68
+ type: nodeType,
69
+ resolve: config.resolveNode,
70
+ description: 'The item at the end of the edge'
71
+ },
72
+ cursor: {
73
+ type: new GraphQLNonNull(GraphQLString),
74
+ resolve: config.resolveCursor,
75
+ description: 'A cursor for use in pagination'
76
+ },
77
+ ...resolveObjMapThunk(config.edgeFields ?? {})
78
+ })
79
+ });
80
+ const connectionType = new GraphQLObjectType({
81
+ name: `${name}Connection`,
82
+ description: 'A connection to a list of items.',
83
+ fields: ()=>({
84
+ pageInfo: {
85
+ type: new GraphQLNonNull(pageInfoType),
86
+ description: 'Information to aid in pagination.'
87
+ },
88
+ edges: {
89
+ type: new GraphQLList(edgeType),
90
+ description: 'A list of edges.'
91
+ },
92
+ ...resolveObjMapThunk(config.connectionFields ?? {})
93
+ })
94
+ });
95
+ return {
96
+ edgeType,
97
+ connectionType
98
+ };
99
+ }
100
+ /**
101
+ * Given a function to map from an ID to an underlying object, and a function to
102
+ * map from an underlying object to its concrete GraphQLObjectType, constructs a
103
+ * `Node` interface plus `node`/`nodes` root field configs.
104
+ */ export function nodeDefinitions(fetchById, typeResolver) {
105
+ const nodeInterface = new GraphQLInterfaceType({
106
+ name: 'Node',
107
+ description: 'An object with an ID',
108
+ fields: ()=>({
109
+ id: {
110
+ type: new GraphQLNonNull(GraphQLID),
111
+ description: 'The id of the object.'
112
+ }
113
+ }),
114
+ resolveType: typeResolver
115
+ });
116
+ const nodeField = {
117
+ description: 'Fetches an object given its ID',
118
+ type: nodeInterface,
119
+ args: {
120
+ id: {
121
+ type: new GraphQLNonNull(GraphQLID),
122
+ description: 'The ID of an object'
123
+ }
124
+ },
125
+ resolve: (_obj, { id }, context, info)=>fetchById(id, context, info)
126
+ };
127
+ const nodesField = {
128
+ description: 'Fetches objects given their IDs',
129
+ type: new GraphQLNonNull(new GraphQLList(nodeInterface)),
130
+ args: {
131
+ ids: {
132
+ type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLID))),
133
+ description: 'The IDs of objects'
134
+ }
135
+ },
136
+ resolve: (_obj, { ids }, context, info)=>ids.map((id)=>fetchById(id, context, info))
137
+ };
138
+ return {
139
+ nodeInterface,
140
+ nodeField,
141
+ nodesField
142
+ };
143
+ }
144
+ function isPromiseLike(value) {
145
+ return typeof value?.then === 'function';
146
+ }
147
+ /**
148
+ * Returns a GraphQLFieldConfig for the mutation described by the provided
149
+ * config, wiring the Relay `clientMutationId` round-trip through a generated
150
+ * `${name}Input`/`${name}Payload` pair.
151
+ */ export function mutationWithClientMutationId(config) {
152
+ const { name, inputFields, outputFields, mutateAndGetPayload } = config;
153
+ const outputType = new GraphQLObjectType({
154
+ name: `${name}Payload`,
155
+ fields: ()=>({
156
+ ...resolveObjMapThunk(outputFields),
157
+ clientMutationId: {
158
+ type: GraphQLString
159
+ }
160
+ })
161
+ });
162
+ const inputType = new GraphQLInputObjectType({
163
+ name: `${name}Input`,
164
+ fields: ()=>({
165
+ ...resolveObjMapThunk(inputFields),
166
+ clientMutationId: {
167
+ type: GraphQLString
168
+ }
169
+ })
170
+ });
171
+ return {
172
+ type: outputType,
173
+ description: config.description,
174
+ deprecationReason: config.deprecationReason,
175
+ extensions: config.extensions,
176
+ args: {
177
+ input: {
178
+ type: new GraphQLNonNull(inputType)
179
+ }
180
+ },
181
+ resolve: (_source, { input }, context, info)=>{
182
+ const { clientMutationId } = input;
183
+ const injectClientMutationId = (data)=>{
184
+ if (typeof data === 'object' && data !== null) {
185
+ ;
186
+ data.clientMutationId = clientMutationId;
187
+ }
188
+ return data;
189
+ };
190
+ const payload = mutateAndGetPayload(input, context, info);
191
+ if (isPromiseLike(payload)) {
192
+ return payload.then((value)=>injectClientMutationId(value));
193
+ }
194
+ return injectClientMutationId(payload);
195
+ }
196
+ };
197
+ }
package/lib/schema.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { AttachmentID, DocumentModelID } from '@kubun/id';
2
2
  import type { AggregateHaving, AggregateSpec, ArrayModel, DocumentData, DocumentModel, DocumentModelSchema, DocumentModelsRecord, DocumentNode, GroupedAggregateOrderTerm, ObjectModel, OrderByDirection as OrderByDirectionValue, ReferencesRecordModel, ScalarModel, StringCustomModel, StringEnumModel, StringFormatModel, StringModel, TypeModel } from '@kubun/protocol';
3
- import { type FieldNode, GraphQLEnumType, type GraphQLFieldConfig, type GraphQLFieldConfigMap, type GraphQLFieldResolver, GraphQLInputObjectType, type GraphQLInputType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, type GraphQLOutputType, type GraphQLResolveInfo, type GraphQLScalarType, GraphQLSchema, type SelectionNode } from 'graphql';
3
+ import { type FieldNode, GraphQLEnumType, type GraphQLFieldConfig, type GraphQLFieldConfigMap, type GraphQLFieldResolver, GraphQLInputObjectType, type GraphQLInputType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, type GraphQLNullableInputType, type GraphQLNullableOutputType, GraphQLObjectType, type GraphQLOutputType, type GraphQLResolveInfo, type GraphQLScalarType, GraphQLSchema, type SelectionNode } from 'graphql';
4
4
  import type { Context } from './context.js';
5
5
  export declare function toDocumentModelID(id: DocumentModelID | string): DocumentModelID;
6
6
  export declare function getGlobalID(id: DocumentModelID | string, globalID: DocumentModelID | string): DocumentModelID;
7
- export declare function toList(type: GraphQLInputType): GraphQLList<GraphQLNonNull<GraphQLInputType>>;
8
- export declare function toOuputList(type: GraphQLOutputType): GraphQLList<GraphQLNonNull<GraphQLOutputType>>;
7
+ export declare function toList(type: GraphQLNullableInputType): GraphQLList<GraphQLNonNull<GraphQLNullableInputType>>;
8
+ export declare function toOuputList(type: GraphQLNullableOutputType): GraphQLList<GraphQLNonNull<GraphQLNullableOutputType>>;
9
9
  export declare const PatchOperation: GraphQLInputObjectType;
10
10
  export type GraphQLNodeDefinitions = {
11
11
  nodeInterface: GraphQLInterfaceType;
@@ -99,18 +99,18 @@ export declare class SchemaBuilder {
99
99
  resolve: (localID: string, ctx: Context) => Promise<unknown> | unknown;
100
100
  }>;
101
101
  _subscriptions: GraphQLFieldConfigMap<unknown, Context>;
102
- _types: Record<string, GraphQLOutputType>;
102
+ _types: Record<string, GraphQLNullableOutputType>;
103
103
  constructor(params: SchemaBuilderParams);
104
104
  _resolveModelIdentifier(identifier: string): string;
105
105
  _resolveModelIDsToInclude(): Set<string>;
106
106
  getModelIDs(id: string | null): Array<string>;
107
107
  build(): GraphQLSchema;
108
108
  _getDefinitions(): SharedDefinitions;
109
- _getReference(path: string, parentName?: string): GraphQLOutputType;
109
+ _getReference(path: string, parentName?: string): GraphQLNullableOutputType;
110
110
  _getReferenceSchema(id: string): TypeModel;
111
111
  _getInputObject(key: string): GraphQLInputObjectType;
112
112
  _getType(key: string): GraphQLOutputType;
113
- _getReferenceInput(path: string, isPartial: boolean, parentName?: string): GraphQLInputType;
113
+ _getReferenceInput(path: string, isPartial: boolean, parentName?: string): GraphQLNullableInputType;
114
114
  _buildSharedDefinitions(): SharedDefinitions;
115
115
  _buildDocument(id: string): GraphQLObjectType<DocumentNode, Context> | GraphQLInterfaceType;
116
116
  /**
@@ -191,9 +191,9 @@ export declare class SchemaBuilder {
191
191
  _buildObjectFilterInput(id: string, schema: DocumentModelSchema | ObjectModel, parentName?: string, isDocument?: boolean): GraphQLInputObjectType;
192
192
  _buildObjectOrderByInput(id: string, schema: DocumentModelSchema | ObjectModel, parentName?: string, isDocument?: boolean): GraphQLInputObjectType;
193
193
  _buildSetInputObjectType(id: string, withFields: Array<string>, relationField?: string): GraphQLInputObjectType;
194
- _buildReference(id: string, schema: TypeModel, parentName?: string): GraphQLOutputType;
194
+ _buildReference(id: string, schema: TypeModel, parentName?: string): GraphQLNullableOutputType;
195
195
  _buildScalar(id: string, schema: ScalarModel, parentName?: string): GraphQLScalarType;
196
- _buildArray(schema: ArrayModel, parentName?: string): GraphQLList<GraphQLNonNull<GraphQLOutputType>>;
196
+ _buildArray(schema: ArrayModel, parentName?: string): GraphQLList<GraphQLNonNull<GraphQLNullableOutputType>>;
197
197
  _buildObject(id: string, schema: ObjectModel, parentName?: string): GraphQLObjectType;
198
198
  _buildObjectFields(id: string, schema: DocumentModelSchema | ObjectModel, parentName?: string): GraphQLFieldConfigMap<Record<string, unknown>, Context>;
199
199
  _buildNestedAttachmentField(key: string, fieldRef: string): GraphQLFieldConfig<Record<string, unknown>, Context> | null;
package/lib/schema.js CHANGED
@@ -2,9 +2,9 @@ import { AttachmentID, DocumentID, DocumentModelID, decodeID, PluginNodeID } fro
2
2
  import { REFERENCE_PREFIX } from '@kubun/protocol';
3
3
  import { pascalCase } from 'change-case';
4
4
  import { extendSchema, GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, isEnumType, isListType, isNonNullType, isObjectType, isScalarType, Kind, parse as parseGraphQL } from 'graphql';
5
- import { connectionArgs, connectionDefinitions, mutationWithClientMutationId, nodeDefinitions } from 'graphql-relay';
6
5
  import { GraphQLDateTimeISO, GraphQLDID, GraphQLDuration, GraphQLEmailAddress, GraphQLJSON, GraphQLJSONObject } from 'graphql-scalars';
7
6
  import { GraphQLAttachmentID, GraphQLCredentialEntryID, GraphQLDocID } from './identifiers.js';
7
+ import { connectionArgs, connectionDefinitions, mutationWithClientMutationId, nodeDefinitions } from './relay.js';
8
8
  import { GraphQLURL } from './scalars.js';
9
9
  import { decodeSearchCursor, encodeSearchCursor } from './search-cursor.js';
10
10
  import { getUniqueDocValue, validateAccessIdentifiers } from './utils.js';
@@ -561,7 +561,7 @@ export class SchemaBuilder {
561
561
  };
562
562
  }
563
563
  // Add top-level search query across all models
564
- // Get the PageInfo type from an existing connection (shared by graphql-relay)
564
+ // Get the PageInfo type from an existing connection (shared across all connections)
565
565
  const firstModelID = [
566
566
  ...this._includedModelIDs
567
567
  ][0];
package/lib/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { DocumentData, DocumentFilter, DocumentOrderBy } from '@kubun/protocol';
2
- import type { ConnectionArguments } from 'graphql-relay';
2
+ import type { ConnectionArguments } from './relay.js';
3
3
  export type PatchAddOperation = {
4
4
  op: 'add';
5
5
  path: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/graphql",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -24,8 +24,7 @@
24
24
  "@sozai/event": "^0.1.3",
25
25
  "@sozai/otel": "^0.3.2",
26
26
  "change-case": "^5.4.4",
27
- "graphql": "^16.14.2",
28
- "graphql-relay": "^0.10.2",
27
+ "graphql": "^17.0.2",
29
28
  "graphql-scalars": "^2.0.0",
30
29
  "multiformats": "^14.0.5"
31
30
  },