Skip to content

Commit bead934

Browse files
authored
chore: Merge PR #52 from feat/add-graphql-union-tuple-support
2 parents 983d144 + 9850043 commit bead934

File tree

6 files changed

+72
-13
lines changed

6 files changed

+72
-13
lines changed

.github/workflows/chromatic.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ jobs:
1919
env:
2020
CHROMATIC_PROJECT_TOKEN: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
2121

22+
- name: Setup Node
23+
if: ${{ steps.check-secrets.outputs.has-secrets == 'true' }}
24+
uses: actions/setup-node@v2
25+
with:
26+
node-version: 18.x
27+
cache: yarn
28+
2229
- name: 'Warn required secrets'
2330
if: ${{ steps.check-secrets.outputs.has-secrets == 'false' }}
2431
run: echo 'CHROMATIC_PROJECT_TOKEN missing in Github secrets. Skipping Storybook deployment.'

apps/next/schema.graphql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
scalar Date
22

3+
"""
4+
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
5+
"""
6+
scalar JSON
7+
8+
"""
9+
The `JSONObject` scalar type represents JSON objects as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
10+
"""
11+
scalar JSONObject
12+
313
input GetResumeDataByUserSlugArgs {
414
"""
515
the unique slug for a user's resume. Can be used to find and retrieve the resume data with

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"packages/*"
1212
],
1313
"engines": {
14-
"node": ">=18.0.0"
14+
"node": ">=18.17.1 <19.0.0"
1515
},
1616
"browser": {
1717
"fs": false,

packages/@aetherspace/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"axios": "^1.4.0",
4646
"graphql": "^16.7.1",
4747
"graphql-request": "^6.1.0",
48+
"graphql-type-json": "^0.3.2",
4849
"swr": "^2.2.0",
4950
"twrnc": "^3.6.1",
5051
"zod": "~3.20.6"

packages/@aetherspace/schemas/aetherGraphSchema.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { gql } from '@apollo/client'
2+
import { GraphQLJSON, GraphQLJSONObject } from 'graphql-type-json'
23
import { aetherSchemaPlugin } from './aetherSchemaPlugin'
34
import { AetherSchemaType } from './aetherSchemas'
5+
import { isEmpty } from '../utils/commonUtils'
46

57
/* --- Scalars --------------------------------------------------------------------------------- */
68

7-
const CUSTOM_SCALARS = ['scalar Date']
9+
const CUSTOM_SCALARS = ['scalar Date', 'scalar JSON', 'scalar JSONObject']
810

911
/* --- Types ----------------------------------------------------------------------------------- */
1012

@@ -123,8 +125,8 @@ const aetherSchemaDefinitions = (aetherSchema: ResolverSchemaType, prefix = 'typ
123125
// -- Arraylikes --
124126
AetherArray: createDefinition('Array'),
125127
// -- Complex types --
126-
// AetherUnion: createDefinition('Union') // TODO: To implement
127-
// AetherTuple: createDefinition('Tuple') // TODO: To implement
128+
AetherUnion: createDefinition('JSON'),
129+
AetherTuple: createDefinition('JSON'),
128130
})
129131
// Transform into usable graphql definitions
130132
const schemaDef = `
@@ -178,7 +180,24 @@ const createResolverDefinition = (resolverConfig: ResolverConfigType) => {
178180

179181
/** --- aetherGraphSchema() -------------------------------------------------------------------- */
180182
/** -i- Turn a mapped object of aetherResolvers into an executable GraphQL schema */
181-
const aetherGraphSchema = (aetherResolvers: ResolverMapType) => {
183+
const aetherGraphSchema = (
184+
aetherResolvers: ResolverMapType,
185+
{
186+
customSchemaDefinitions = '',
187+
customQueryDefinitions = '',
188+
customMutationDefinitions = '',
189+
customScalars = {},
190+
customQueries = {},
191+
customMutations = {},
192+
}: {
193+
customSchemaDefinitions?: string
194+
customQueryDefinitions?: string
195+
customMutationDefinitions?: string
196+
customScalars?: Record<string, (...args: unknown[]) => Promise<unknown>>
197+
customQueries?: Record<string, (...args: unknown[]) => Promise<unknown>>
198+
customMutations?: Record<string, (...args: unknown[]) => Promise<unknown>>
199+
} = {}
200+
) => {
182201
const resolverEntries = Object.entries(aetherResolvers)
183202
const resolverConfigs = resolverEntries.map(([resolverName, resolver]) => ({
184203
resolverName,
@@ -187,27 +206,44 @@ const aetherGraphSchema = (aetherResolvers: ResolverMapType) => {
187206
isMutation: !!resolver?.isMutation,
188207
resolver,
189208
}))
209+
190210
const mutationConfigs = resolverConfigs.filter((resolverConfig) => resolverConfig.isMutation)
211+
const mutationDefs = [customMutationDefinitions, ...mutationConfigs.map(createResolverDefinition)].filter(Boolean) // prettier-ignore
212+
const hasMutations = mutationDefs.length > 0 || !isEmpty(customMutations)
213+
191214
const queryConfigs = resolverConfigs.filter((resolverConfig) => !resolverConfig.isMutation)
192-
const dataTypeDefs = Array.from(new Set(aetherGraphDefinitions(resolverConfigs)))
193-
const mutationDefs = mutationConfigs.map(createResolverDefinition)
194-
const queryDefs = queryConfigs.map(createResolverDefinition)
195-
const hasMutations = mutationDefs.length > 0
196-
const hasQueries = queryDefs.length > 0
215+
const queryDefs = [customQueryDefinitions, ...queryConfigs.map(createResolverDefinition)].filter(Boolean) // prettier-ignore
216+
const hasQueries = queryDefs.length > 0 || !isEmpty(customQueries)
217+
197218
const mutation = hasMutations ? `type Mutation {\n ${mutationDefs.join('\n ')}\n}` : ''
198219
const query = hasQueries ? `type Query {\n ${queryDefs.join('\n ')}\n}` : ''
199-
const allTypeDefs = [...CUSTOM_SCALARS, ...dataTypeDefs, mutation, query].filter(Boolean)
220+
221+
const dataTypeDefs = Array.from(new Set(aetherGraphDefinitions(resolverConfigs)))
222+
const allTypeDefs = [
223+
customSchemaDefinitions,
224+
...CUSTOM_SCALARS,
225+
...dataTypeDefs,
226+
mutation,
227+
query,
228+
].filter(Boolean)
229+
200230
const typeDefsString = allTypeDefs.join('\n\n')
201231
const graphqlSchemaDefs = gql`${typeDefsString}` // prettier-ignore
232+
202233
const rebuildFromConfig = (handlers, { resolverName, resolver }) => ({
203234
...handlers,
204235
[resolverName]: resolver,
205236
})
206-
const queryResolvers = queryConfigs.reduce(rebuildFromConfig, {})
207-
const mutationResolvers = mutationConfigs.reduce(rebuildFromConfig, {})
237+
238+
const queryResolvers = queryConfigs.reduce(rebuildFromConfig, customQueries)
239+
const mutationResolvers = mutationConfigs.reduce(rebuildFromConfig, customMutations)
240+
208241
return {
209242
typeDefs: graphqlSchemaDefs,
210243
resolvers: {
244+
JSON: GraphQLJSON,
245+
JSONObject: GraphQLJSONObject,
246+
...customScalars,
211247
...(hasQueries ? { Query: queryResolvers } : {}),
212248
...(hasMutations ? { Mutations: mutationResolvers } : {}),
213249
},

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10760,6 +10760,11 @@ graphql-tag@^2.10.1, graphql-tag@^2.12.6:
1076010760
dependencies:
1076110761
tslib "^2.1.0"
1076210762

10763+
graphql-type-json@^0.3.2:
10764+
version "0.3.2"
10765+
resolved "https://registry.yarnpkg.com/graphql-type-json/-/graphql-type-json-0.3.2.tgz#f53a851dbfe07bd1c8157d24150064baab41e115"
10766+
integrity sha512-J+vjof74oMlCWXSvt0DOf2APEdZOCdubEvGDUAlqH//VBYcOYsGgRW7Xzorr44LvkjiuvecWc8fChxuZZbChtg==
10767+
1076310768
graphql@15.8.0:
1076410769
version "15.8.0"
1076510770
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.8.0.tgz#33410e96b012fa3bdb1091cc99a94769db212b38"

0 commit comments

Comments
 (0)