@xyd-js/gql 0.1.0-xyd.0 → 0.1.0-xyd.10

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.
@@ -0,0 +1,28 @@
1
+ """
2
+ This is a custom scalar type for Date.
3
+ """
4
+ scalar Date
5
+
6
+ """
7
+ This is a custom enum type for Role.
8
+ """
9
+ enum Role {
10
+ ADMIN
11
+ OWNER
12
+ }
13
+
14
+ """
15
+ This is a custom input type for Book.
16
+ """
17
+ input BookInput {
18
+ title: String!
19
+ author: String!
20
+ }
21
+
22
+ """
23
+ This is a custom type for Book.
24
+ """
25
+ type Book {
26
+ title: String!
27
+ author: String!
28
+ }
@@ -0,0 +1,8 @@
1
+ input BookInput {
2
+ title: String!
3
+ author: String!
4
+ }
5
+
6
+ type Mutation {
7
+ addBook(bookInput: BookInput): Boolean!
8
+ }
@@ -0,0 +1,8 @@
1
+ input BookInput {
2
+ title: String!
3
+ author: String!
4
+ }
5
+
6
+ type Mutation {
7
+ addBook(bookInput: BookInput!): Boolean!
8
+ }
@@ -0,0 +1,12 @@
1
+ input BookInput {
2
+ title: String!
3
+ author: Author!
4
+ }
5
+
6
+ input Author {
7
+ name: String!
8
+ }
9
+
10
+ type Mutation {
11
+ addBook(bookInput: BookInput): Boolean!
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xyd-js/gql",
3
- "version": "0.1.0-xyd.0",
3
+ "version": "0.1.0-xyd.10",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -11,16 +11,21 @@
11
11
  "graphql-config": "^5.1.2",
12
12
  "gray-matter": "^4.0.3",
13
13
  "json-to-graphql-query": "^2.3.0",
14
- "@xyd-js/core": "0.1.0-xyd.0",
15
- "@xyd-js/uniform": "0.1.0-xyd.2"
14
+ "@xyd-js/core": "0.1.0-xyd.10",
15
+ "@xyd-js/uniform": "0.1.0-xyd.12"
16
16
  },
17
17
  "devDependencies": {
18
- "tsup": "^8.3.0"
18
+ "rimraf": "^3.0.2",
19
+ "tsup": "^8.3.0",
20
+ "typescript": "^4.5.5",
21
+ "vitest": "^2.1.1"
19
22
  },
20
23
  "scripts": {
21
24
  "clean": "rimraf build",
22
25
  "prebuild": "pnpm clean",
23
26
  "build": "tsup",
24
- "build:examples": "tsup --config tsup.examples-config.ts"
27
+ "build:examples": "tsup --config tsup.examples-config.ts",
28
+ "test": "vitest",
29
+ "ci:test": "vitest run"
25
30
  }
26
31
  }
@@ -0,0 +1 @@
1
+ `hydration` converts a GraphQL into XYD `uniform` format.
@@ -0,0 +1,53 @@
1
+ import {GraphQLArgument} from "graphql/type/definition";
2
+ import {GraphQLInputObjectType} from "graphql/type";
3
+
4
+ import {DefinitionProperty} from "@xyd-js/uniform";
5
+
6
+ import {gqlInputToUniformDefinitionProperty} from "./gql-input";
7
+
8
+ // gqlArgToUniformDefinitionProperty converts GraphQL arguments into xyd 'uniform' definition properties
9
+ export function gqlArgToUniformDefinitionProperty(
10
+ args: readonly GraphQLArgument[]
11
+ ): DefinitionProperty[] {
12
+ const resp: DefinitionProperty[] = []
13
+
14
+ args.forEach(arg => {
15
+ let obj: GraphQLInputObjectType | null = null
16
+
17
+ switch (arg.type.constructor.name) {
18
+ case "GraphQLNonNull": {
19
+ if ("ofType" in arg.type) {
20
+ const ofType = arg.type?.ofType
21
+
22
+ if (ofType?.constructor.name === "GraphQLInputObjectType") {
23
+ obj = ofType as GraphQLInputObjectType
24
+ }
25
+ }
26
+ break
27
+ }
28
+
29
+ case "GraphQLInputObjectType" : {
30
+ obj = arg.type as GraphQLInputObjectType
31
+
32
+ break
33
+ }
34
+
35
+ default: {
36
+ }
37
+ }
38
+
39
+ if (!obj) {
40
+ console.error("unsupported argument type", arg.type.constructor.name)
41
+ return
42
+ }
43
+
44
+ resp.push(gqlInputToUniformDefinitionProperty(
45
+ arg.name,
46
+ arg.description || "",
47
+ obj
48
+ ))
49
+ })
50
+
51
+ return resp
52
+ }
53
+
@@ -1,4 +1,3 @@
1
- // overFields iterates over fields of object or input object types
2
1
  import {
3
2
  GraphQLField,
4
3
  GraphQLFieldMap,
@@ -6,15 +5,18 @@ import {
6
5
  GraphQLInputFieldMap,
7
6
  GraphQLInputObjectType
8
7
  } from "graphql/type";
9
- import {GraphQLObjectType} from "graphql";
8
+ import {GraphQLObjectType, GraphQLNamedType} from "graphql";
9
+
10
10
  import {DefinitionProperty} from "@xyd-js/uniform";
11
+ import {isIntrospectionType, isSpecifiedScalarType} from "graphql/index";
11
12
 
12
- // fieldIntoDefinitionProperty converts GraphQL fields (field or input field) into xyd 'uniform' definition property
13
- export function fieldIntoDefinitionProperty(
13
+ // gqlFieldToUniformDefinitionProperty converts GraphQL fields (field or input field) into xyd 'uniform' definition property
14
+ export function gqlFieldToUniformDefinitionProperty(
14
15
  fieldName: string,
15
16
  field: GraphQLField<any, any> | GraphQLInputField,
16
17
  ): DefinitionProperty {
17
18
  let properties;
19
+ let graphqlTypeFlat: GraphQLNamedType | null = null
18
20
 
19
21
  // if 'ofType' types (non-null values e.g '!<type>')
20
22
  if ("ofType" in field.type) {
@@ -25,6 +27,7 @@ export function fieldIntoDefinitionProperty(
25
27
  const objectType = field.type.ofType as GraphQLObjectType
26
28
 
27
29
  properties = nestedProperties(objectType)
30
+ graphqlTypeFlat = objectType
28
31
 
29
32
  break
30
33
  }
@@ -33,12 +36,14 @@ export function fieldIntoDefinitionProperty(
33
36
  const inputObjectType = field.type.ofType as GraphQLInputObjectType
34
37
 
35
38
  properties = nestedProperties(inputObjectType)
39
+ graphqlTypeFlat = inputObjectType
36
40
 
37
41
  break
38
42
  }
39
43
 
40
- case "GraphQLScalarType" : {
44
+ case "GraphQLScalarType": {
41
45
  properties = definitionPropsFromNestedObj(field) || []
46
+ graphqlTypeFlat = field.type.ofType as GraphQLNamedType
42
47
 
43
48
  break
44
49
  }
@@ -46,6 +51,10 @@ export function fieldIntoDefinitionProperty(
46
51
  case "GraphQLNonNull": {
47
52
  properties = definitionPropsFromNestedObj(field) || []
48
53
 
54
+ if ("ofType" in field.type.ofType) {
55
+ graphqlTypeFlat = field.type.ofType.ofType as GraphQLNamedType
56
+ }
57
+
49
58
  break
50
59
  }
51
60
 
@@ -61,6 +70,7 @@ export function fieldIntoDefinitionProperty(
61
70
 
62
71
  case "GraphQLNonNull": {
63
72
  properties = definitionPropsFromNestedObj(field) || []
73
+ graphqlTypeFlat = field.type.ofType as GraphQLNamedType
64
74
 
65
75
  break
66
76
  }
@@ -77,6 +87,7 @@ export function fieldIntoDefinitionProperty(
77
87
  // if regular object type
78
88
  else if (field.type.constructor.name === "GraphQLObjectType") {
79
89
  const objectType = field.type as GraphQLObjectType
90
+ graphqlTypeFlat = field.type
80
91
 
81
92
  // TODO: support nested & circular references - ITS JUST A FAST SOLUTION FOR TESTING PURPOSES
82
93
  // properties = [
@@ -89,18 +100,57 @@ export function fieldIntoDefinitionProperty(
89
100
 
90
101
  // TODO: comment if bug with circular references
91
102
  properties = nestedProperties(objectType)
92
- }
93
-
94
- // if input object type
95
- else if (field.type.constructor.name === "GraphQLInputObjectType") {
103
+ } else if (field.type.constructor.name === "GraphQLInputObjectType") {
96
104
  const objectType = field.type as GraphQLInputObjectType
97
105
 
106
+ graphqlTypeFlat = field.type
98
107
  properties = nestedProperties(objectType)
108
+ } else if (field.type.constructor.name === "GraphQLScalarType") {
109
+ graphqlTypeFlat = field.type
110
+ }
111
+
112
+
113
+ switch (graphqlTypeFlat?.constructor?.name) {
114
+ case "GraphQLList": {
115
+ if ("ofType" in graphqlTypeFlat) {
116
+ graphqlTypeFlat = graphqlTypeFlat.ofType as GraphQLNamedType
117
+ }
118
+ }
119
+ }
120
+
121
+ const builtInType = graphqlTypeFlat ? (isSpecifiedScalarType(graphqlTypeFlat) ||
122
+ isIntrospectionType(graphqlTypeFlat)
123
+ ) : undefined
124
+
125
+ let graphqlTypeShort = ""
126
+
127
+ switch (graphqlTypeFlat?.constructor?.name) {
128
+ case "GraphQLObjectType": {
129
+ graphqlTypeShort = "object"
130
+
131
+ break
132
+ }
133
+ case "GraphQLInputObjectType": {
134
+ graphqlTypeShort = "input"
135
+
136
+ break
137
+ }
138
+ case "GraphQLScalarType": {
139
+ graphqlTypeShort = "scalar"
140
+
141
+ break
142
+ }
99
143
  }
100
144
 
101
145
  return {
102
146
  name: fieldName,
103
147
  type: field.type.toJSON(),
148
+ context: {
149
+ graphqlBuiltInType: builtInType,
150
+ graphqlName: fieldName,
151
+ graphqlTypeFlat: graphqlTypeFlat && graphqlTypeFlat.toJSON(),
152
+ graphqlTypeShort,
153
+ },
104
154
  description: field.description || "",
105
155
  properties,
106
156
  }
@@ -134,7 +184,7 @@ function deepFieldMap(
134
184
  const properties: DefinitionProperty[] = []
135
185
 
136
186
  for (const [name, field] of Object.entries(fieldsMap)) {
137
- const prop = fieldIntoDefinitionProperty(
187
+ const prop = gqlFieldToUniformDefinitionProperty(
138
188
  name,
139
189
  field,
140
190
  )
@@ -0,0 +1,67 @@
1
+ import {GraphQLInputObjectType} from "graphql/type";
2
+
3
+ import {DefinitionProperty, Reference} from "@xyd-js/uniform";
4
+
5
+ import {gqlFieldToUniformDefinitionProperty} from "./gql-field";
6
+
7
+ // gqlInputToUniformRef is a helper function to convert a GraphQL input object type into a 'uniform' reference.
8
+ export function gqlInputToUniformRef(gqlType: GraphQLInputObjectType): Reference {
9
+ const prop = gqlInputToUniformDefinitionProperty(
10
+ gqlType.name,
11
+ gqlType.description || "",
12
+ gqlType
13
+ )
14
+
15
+ return {
16
+ title: gqlType.name,
17
+ description: gqlType.description || "",
18
+ canonical: `input-${gqlType.name}`, // TODO: better solution
19
+ context: {
20
+ graphqlName: gqlType.name,
21
+ graphqlTypeShort: "input" // TODO: better solution
22
+ },
23
+ definitions: [
24
+ {
25
+ title: "Fields",
26
+ properties: prop.properties || []
27
+ }
28
+ ],
29
+ examples: {
30
+ groups: []
31
+ }
32
+ } as Reference
33
+ }
34
+
35
+ // gqlInputToUniformDefinitionProperty is a helper function to convert a GraphQL input object into a xyd definition property.
36
+ export function gqlInputToUniformDefinitionProperty(
37
+ name: string,
38
+ description: string,
39
+ obj: GraphQLInputObjectType
40
+ ) {
41
+ const inputFields = obj.getFields?.()
42
+
43
+ const nestedProps: DefinitionProperty[] = []
44
+ const nestedDefinitionProperty: DefinitionProperty = {
45
+ name: name,
46
+ type: obj.toJSON(),
47
+ description: description || "",
48
+ context: {
49
+ graphqlName: name,
50
+ graphqlTypeShort: "input"
51
+ },
52
+ properties: nestedProps,
53
+ }
54
+
55
+ for (const [name, inputField] of Object.entries(inputFields)) {
56
+ const prop = gqlFieldToUniformDefinitionProperty(
57
+ name,
58
+ inputField,
59
+ )
60
+
61
+ if (prop) {
62
+ nestedProps.push(prop)
63
+ }
64
+ }
65
+
66
+ return nestedDefinitionProperty
67
+ }
@@ -0,0 +1,35 @@
1
+ import {GraphQLObjectType} from "graphql";
2
+
3
+ import type {DefinitionProperty} from "@xyd-js/uniform";
4
+
5
+ import {gqlFieldToUniformDefinitionProperty} from "./gql-field";
6
+
7
+ // gqlObjectToUniformRef is a helper function to convert a GraphQL object type into a 'uniform' reference.
8
+ export function gqlObjectToUniformRef(gqlType: GraphQLObjectType) {
9
+ const props: DefinitionProperty[] = []
10
+
11
+ for (const [name, field] of Object.entries(gqlType.getFields())) {
12
+ const prop = gqlFieldToUniformDefinitionProperty(name, field)
13
+
14
+ props.push(prop)
15
+ }
16
+
17
+ return {
18
+ title: gqlType.name,
19
+ description: gqlType.description || "",
20
+ canonical: `object-${gqlType.name}`, // TODO: better solution
21
+ context: {
22
+ graphqlName: gqlType.name,
23
+ graphqlTypeShort: "object" // TODO: better solution
24
+ },
25
+ definitions: [
26
+ {
27
+ title: "Fields",
28
+ properties: props
29
+ }
30
+ ],
31
+ examples: {
32
+ groups: []
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,50 @@
1
+ import {GraphQLEnumType} from "graphql";
2
+
3
+ import {GraphQLScalarType} from "@graphql-markdown/types";
4
+
5
+ import type {DefinitionProperty, Reference} from "@xyd-js/uniform";
6
+
7
+ // gqlEnumToUniformRef is a helper function to convert a GraphQL enum type into a 'uniform' reference.
8
+ export function gqlEnumToUniformRef(gqlType: GraphQLEnumType): Reference {
9
+ const props: DefinitionProperty[] = gqlType.getValues().map(value => ({
10
+ name: value.name,
11
+ type: "string",
12
+ description: value.description || "",
13
+ }))
14
+
15
+ return {
16
+ title: gqlType.name,
17
+ description: gqlType.description || "",
18
+ canonical: `enum-${gqlType.name}`, // TODO: better solution
19
+ context: {
20
+ graphqlName: gqlType.name,
21
+ graphqlTypeShort: "enum" // TODO: better solution
22
+ },
23
+ definitions: [
24
+ {
25
+ title: "Valid values",
26
+ properties: props
27
+ }
28
+ ],
29
+ examples: {
30
+ groups: []
31
+ }
32
+ } as Reference
33
+ }
34
+
35
+ // gqlScalarToUniformRef is a helper function to convert a GraphQL scalar type into a 'uniform' reference.
36
+ export function gqlScalarToUniformRef(gqlType: GraphQLScalarType): Reference {
37
+ return {
38
+ title: gqlType.name,
39
+ description: gqlType.description || "",
40
+ canonical: `scalar-${gqlType.name}`, // TODO: better solution
41
+ context: {
42
+ graphqlName: gqlType.name,
43
+ graphqlTypeShort: "scalar" // TODO: better solution
44
+ },
45
+ definitions: [],
46
+ examples: {
47
+ groups: []
48
+ }
49
+ } as Reference
50
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export {
2
- gqlSchemaToReferences
2
+ gqlSchemaToReferences // TODO: rename to gqlSchemaToUniformRef ?
3
3
  } from "./schema"
@@ -1,10 +1,10 @@
1
1
  import {jsonToGraphQLQuery, VariableType} from "json-to-graphql-query";
2
+
2
3
  import {
3
4
  DefinitionProperty,
4
5
  ReferenceType
5
6
  } from "@xyd-js/uniform";
6
7
 
7
- // TODO: support args
8
8
  // simpleGraphqlExample is a helper function to create a simple GraphQL example query or mutation.
9
9
  export function simpleGraphqlExample(
10
10
  operationType: ReferenceType.GRAPHQL_QUERY | ReferenceType.GRAPHQL_MUTATION,
@@ -14,7 +14,6 @@ export function simpleGraphqlExample(
14
14
  ) {
15
15
  let obj: any = {}
16
16
 
17
-
18
17
  switch (operationType) {
19
18
  case ReferenceType.GRAPHQL_QUERY: {
20
19
  const exampleReturnProps = exampleReturns(returns)
package/src/schema.ts CHANGED
@@ -1,10 +1,21 @@
1
- import {getDocumentLoaders, loadSchema} from "@graphql-markdown/graphql";
1
+ import {
2
+ GraphQLInputObjectType,
3
+ GraphQLObjectType,
4
+ GraphQLEnumType,
5
+ isSpecifiedScalarType,
6
+ isIntrospectionType
7
+ } from "graphql";
2
8
  import {OperationTypeNode} from "graphql/language/ast";
3
- import {Reference, ReferenceType} from "@xyd-js/uniform"
4
9
 
5
- import {
6
- graphqlOperationReferences
7
- } from "./utils";
10
+ import {getDocumentLoaders, loadSchema} from "@graphql-markdown/graphql";
11
+ import {GraphQLScalarType} from "@graphql-markdown/types";
12
+
13
+ import type {Reference} from "@xyd-js/uniform"
14
+ import {ReferenceType} from "@xyd-js/uniform"
15
+ import {gqlInputToUniformRef} from "./hydration/gql-input";
16
+ import {gqlEnumToUniformRef, gqlScalarToUniformRef} from "./hydration/gql-types";
17
+ import {gqlObjectToUniformRef} from "./hydration/gql-object";
18
+ import {gqlOperationsToUniformRef} from "./utils";
8
19
 
9
20
  // TODO: support multi graphql files
10
21
  // TODO: !!! CIRCULAR_DEPENDENCY !!!
@@ -27,7 +38,7 @@ export async function gqlSchemaToReferences(
27
38
  const queryFields = queries?.getFields?.()
28
39
 
29
40
  if (queryFields) {
30
- references.push(...graphqlOperationReferences(
41
+ references.push(...gqlOperationsToUniformRef(
31
42
  ReferenceType.GRAPHQL_QUERY,
32
43
  queryFields
33
44
  ))
@@ -37,11 +48,60 @@ export async function gqlSchemaToReferences(
37
48
  const mutationFields = mutations?.getFields?.()
38
49
 
39
50
  if (mutationFields) {
40
- references.push(...graphqlOperationReferences(
51
+ references.push(...gqlOperationsToUniformRef(
41
52
  ReferenceType.GRAPHQL_MUTATION,
42
53
  mutationFields
43
54
  ))
44
55
  }
45
56
 
57
+ const typeMap = schema.getTypeMap();
58
+
59
+ for (const gqlType of Object.values(typeMap)) {
60
+ const builtInType = isSpecifiedScalarType(gqlType) ||
61
+ isIntrospectionType(gqlType) ||
62
+ gqlType.name === "Mutation"
63
+
64
+ if (builtInType) {
65
+ continue;
66
+ }
67
+
68
+ switch (gqlType.constructor.name) {
69
+ case 'GraphQLObjectType': {
70
+ const type = gqlType as GraphQLObjectType;
71
+
72
+ references.push(gqlObjectToUniformRef(type))
73
+
74
+ break
75
+ }
76
+
77
+ case 'GraphQLInputObjectType': {
78
+ const type = gqlType as GraphQLInputObjectType;
79
+
80
+ references.push(gqlInputToUniformRef(type))
81
+ break
82
+ }
83
+
84
+ case 'GraphQLEnumType': {
85
+ const type = gqlType as GraphQLEnumType;
86
+
87
+ references.push(gqlEnumToUniformRef(type))
88
+
89
+ break
90
+ }
91
+
92
+ case 'GraphQLScalarType': {
93
+ const type = gqlType as GraphQLScalarType
94
+
95
+ references.push(gqlScalarToUniformRef(type))
96
+
97
+ break
98
+ }
99
+
100
+ default: {
101
+ console.debug(`Unsupported type: ${gqlType.constructor.name}`)
102
+ }
103
+ }
104
+ }
105
+
46
106
  return references
47
107
  }
package/src/utils.ts CHANGED
@@ -8,13 +8,12 @@ import {
8
8
  Example,
9
9
  } from "@xyd-js/uniform";
10
10
 
11
- import {argumentsIntoDefinitionProperty} from "./arguments";
12
- import {fieldIntoDefinitionProperty} from "./fields";
13
- import {simpleGraphqlExample} from "./examples";
11
+ import {gqlArgToUniformDefinitionProperty} from "./hydration/gql-arg";
12
+ import {gqlFieldToUniformDefinitionProperty} from "./hydration/gql-field";
13
+ import {simpleGraphqlExample} from "./samples";
14
14
 
15
- // TODO: examples
16
- // graphqlOperationReferences is a helper function to create a list of xyd reference for a GraphQL query or mutation.
17
- export function graphqlOperationReferences(
15
+ // gqlOperationsToUniformRef is a helper function to create a list of xyd reference for a GraphQL query or mutation.
16
+ export function gqlOperationsToUniformRef(
18
17
  operationType: ReferenceType.GRAPHQL_MUTATION | ReferenceType.GRAPHQL_QUERY,
19
18
  fieldsMap: GraphQLFieldMap<any, any>
20
19
  ) {
@@ -23,8 +22,8 @@ export function graphqlOperationReferences(
23
22
  for (const [operationName, operationField] of Object.entries(fieldsMap)) {
24
23
  const definitions: Definition[] = []
25
24
 
26
- const args = argumentsIntoDefinitionProperty(operationField.args)
27
- const returns = fieldIntoDefinitionProperty(operationName, operationField)
25
+ const args = gqlArgToUniformDefinitionProperty(operationField.args)
26
+ const returns = gqlFieldToUniformDefinitionProperty(operationName, operationField)
28
27
  const returnProperties = returns.properties || []
29
28
 
30
29
  definitions.push({