Skip to content

Commit

Permalink
feat: add subscribeToMore support
Browse files Browse the repository at this point in the history
  • Loading branch information
playerx committed Oct 15, 2020
1 parent d2278f2 commit 9b5f863
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 285 deletions.
34 changes: 34 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Add files here to ignore them from prettier formatting
dist/**/*
out/**/*
generated/**/*
**/*.gen.*
node_modules/**/*
coverage/**/*
*.snap

*.sh
*.png
*.gif
*.ico
*.icns
*.svg
*.txt
*.mp3

**/.git*
**/.DS_Store
**/.prettier*
**/.env*
**/.nvm
**/*.conf
**/browserslist

# custom
.generated/*
capacitor/*
tools/*
**/assets/*
.editorconfig
apple-app-site-association
yarn.lock
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
"endOfLine": "auto",
"semi": false,
"printWidth": 70,
"arrowParens": "avoid",
"bracketSpacing": true,
"quoteProps": "as-needed",
"trailingComma": "all"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,89 @@ import renderApolloCall from '../render/renderApolloCall'
import renderFragment from '../render/renderFragment'
import renderMethod from '../render/renderMethod'
import renderOptions from '../render/renderOptions'
import renderReturnDocument from '../render/renderReturnDocument'
import renderSubscription from '../render/renderSubscription'

export default function (field: IntrospectionField, types: IntrospectionType[], generateDefaultFragments: boolean) {
export default function (
field: IntrospectionField,
types: IntrospectionType[],
generateDefaultFragments: boolean,
onlyDocument: boolean,
) {
const queryName = field.name
const propsType = getTypescriptPropsTypeName(
'Subscription',
queryName,
)
const inputs = field.args || []
const methodName = uncapitalizeFirstLetter(queryName)
const hasInputs = !!inputs.length

const queryName = field.name
const propsType = getTypescriptPropsTypeName('Subscription', queryName)
const inputs = field.args || []
const methodName = uncapitalizeFirstLetter(queryName)
const hasInputs = !!inputs.length
const returnClassFullname = getTypescriptTypeString({
type: field.type,
})

const returnClassFullname = getTypescriptTypeString({
type: field.type,
})
const returnClassName = getGraphqlTypeString({
type: field.type,
capitalizeName: true,
onlyName: true,
})

const returnClassName = getGraphqlTypeString({
type: field.type,
capitalizeName: true,
onlyName: true,
})
const returnGraphqlTypeName = returnClassName
const fragmentName = returnClassName + 'Data'

const returnGraphqlTypeName = returnClassName
const fragmentName = returnClassName + 'Data'
const props = generatePropsType(propsType, inputs)

const props = generatePropsType(propsType, inputs)
const resultTypeFields = generateResultTypeFields(field.type, types)
const hasResultType = !!resultTypeFields

const resultTypeFields = generateResultTypeFields(field.type, types)
const hasResultType = !!resultTypeFields
const variablesDeclarationString = buildVariablesDeclarationString(
inputs,
)
const variablesString = buildVariablesPassString(inputs)

const variablesDeclarationString = buildVariablesDeclarationString(inputs)
const variablesString = buildVariablesPassString(inputs)
const type = <IntrospectionType>(
types.find(x => x.name === returnGraphqlTypeName)
)

const type = <IntrospectionType> types.find(x => x.name === returnGraphqlTypeName)
const method = renderMethod({
rootType: RootType.Subscription,
methodName,
generateDefaultFragments,
hasProps: hasInputs,
propsType,
hasResultType,
renderContent: () =>
// Render Query
renderOptions(fragmentName, hasResultType) +
(hasResultType
? renderFragment(
type,
generateDefaultFragments,
returnGraphqlTypeName,
)
: '') +
renderSubscription({
hasFragment: hasResultType,
queryName,
variablesDeclarationString,
variablesString,
}) +
(onlyDocument
? renderReturnDocument({
rootType: RootType.Subscription,
hasVariables: hasInputs,
})
: renderApolloCall({
rootType: RootType.Subscription,
hasVariables: hasInputs,
queryName,
returnType: returnClassFullname,
})),
})

const method = renderMethod({
rootType: RootType.Subscription,
methodName,
generateDefaultFragments,
hasProps: hasInputs,
propsType,
hasResultType,
renderContent: () =>
// Render Query
renderOptions(fragmentName, hasResultType) +
(hasResultType
? renderFragment(type, generateDefaultFragments, returnGraphqlTypeName)
: '') +
renderSubscription({
hasFragment: hasResultType,
queryName,
variablesDeclarationString,
variablesString,
}) +
renderApolloCall({
rootType: RootType.Subscription,
hasVariables: hasInputs,
queryName,
returnType: returnClassFullname,
}),
})

return {
method,
props,
}
return {
method,
props,
}
}
179 changes: 110 additions & 69 deletions src/commands/graphql-client/generator/generateProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,126 @@ import renderProxy from './render/renderProxy'
import renderQueryTypesEnum from './render/renderQueryTypesEnum'

export default function (
introspectionSchema: IntrospectionSchema,
generateDefaultFragments: boolean,
introspectionSchema: IntrospectionSchema,
generateDefaultFragments: boolean,
) {
const {
queryType: { name: queryTypeName },
types,
}: IntrospectionSchema = introspectionSchema

const mutationTypeName = introspectionSchema.mutationType
? introspectionSchema.mutationType.name
: null

const subscriptionTypeName = introspectionSchema.subscriptionType
? introspectionSchema.subscriptionType.name
: null

const queryType = <IntrospectionType>types.find(x => x.name === queryTypeName)
const mutationType = <IntrospectionType>types.find(x => x.name === mutationTypeName)
const subscriptionType = <IntrospectionType>types.find(x => x.name === subscriptionTypeName)

const otherTypes = types.filter(x =>
(x !== queryType) &&
(x !== mutationType) &&
(x !== subscriptionType),
)

const objectTypes = otherTypes.filter(x => x.kind === 'OBJECT')

// start generation
const generatedQuery = generateRootType(RootType.Query, otherTypes, generateDefaultFragments)(queryType)
const generatedWatchQuery = generateRootType('watchQuery', otherTypes, generateDefaultFragments)(queryType)
const generatedRefetchQuery = generateRootType('refetchQuery', otherTypes, generateDefaultFragments)(queryType)
const generatedCacheWriteQuery = generateRootType('cacheWriteQuery', otherTypes, generateDefaultFragments)(queryType)
const generatedMutation = generateRootType(RootType.Mutation, otherTypes, generateDefaultFragments)(mutationType)
const generatedSubscription =
generateRootType(RootType.Subscription, otherTypes, generateDefaultFragments)(subscriptionType)

const generatedOtherTypes = otherTypes
.sort(sortTypesByKind)
.map(generateType)
.filter(x => !!x)
.join('\n')

const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes)

return renderProxy({
generatedQuery,
generatedWatchQuery,
generatedRefetchQuery,
generatedCacheWriteQuery,
generatedMutation,
generatedSubscription,
generatedOtherTypes,
generatedQueryTypesEnum,
})
const {
queryType: { name: queryTypeName },
types,
}: IntrospectionSchema = introspectionSchema

const mutationTypeName = introspectionSchema.mutationType
? introspectionSchema.mutationType.name
: null

const subscriptionTypeName = introspectionSchema.subscriptionType
? introspectionSchema.subscriptionType.name
: null

const queryType = <IntrospectionType>(
types.find(x => x.name === queryTypeName)
)
const mutationType = <IntrospectionType>(
types.find(x => x.name === mutationTypeName)
)
const subscriptionType = <IntrospectionType>(
types.find(x => x.name === subscriptionTypeName)
)

const otherTypes = types.filter(
x =>
x !== queryType && x !== mutationType && x !== subscriptionType,
)

const objectTypes = otherTypes.filter(x => x.kind === 'OBJECT')

// start generation
const generatedQuery = generateRootType(
RootType.Query,
otherTypes,
generateDefaultFragments,
)(queryType)

const generatedWatchQuery = generateRootType(
'watchQuery',
otherTypes,
generateDefaultFragments,
)(queryType)

const generatedRefetchQuery = generateRootType(
'refetchQuery',
otherTypes,
generateDefaultFragments,
)(queryType)

const generatedCacheWriteQuery = generateRootType(
'cacheWriteQuery',
otherTypes,
generateDefaultFragments,
)(queryType)

const generatedMutation = generateRootType(
RootType.Mutation,
otherTypes,
generateDefaultFragments,
)(mutationType)

const generatedSubscription = generateRootType(
RootType.Subscription,
otherTypes,
generateDefaultFragments,
)(subscriptionType)

const generatedSubscriptionDocument = generateRootType(
RootType.Subscription,
otherTypes,
generateDefaultFragments,
true,
)(subscriptionType)

const generatedOtherTypes = otherTypes
.sort(sortTypesByKind)
.map(generateType)
.filter(x => !!x)
.join('\n')

const generatedQueryTypesEnum = renderQueryTypesEnum(objectTypes)

return renderProxy({
generatedQuery,
generatedWatchQuery,
generatedRefetchQuery,
generatedCacheWriteQuery,
generatedMutation,
generatedSubscription,
generatedSubscriptionDocument,
generatedOtherTypes,
generatedQueryTypesEnum,
})
}

function sortTypesByKind(a: IntrospectionType, b: IntrospectionType) {
return getSortValue(b) - getSortValue(a)
return getSortValue(b) - getSortValue(a)
}

function getSortValue(x: IntrospectionType) {
switch (x.kind) {
case 'SCALAR':
return 10
switch (x.kind) {
case 'SCALAR':
return 10

case 'UNION':
return 9
case 'UNION':
return 9

case 'ENUM':
return 8
case 'ENUM':
return 8

case 'INPUT_OBJECT':
return 7
case 'INPUT_OBJECT':
return 7

case 'OBJECT':
return 6
case 'OBJECT':
return 6

default:
return 0
}
default:
return 0
}
}
Loading

0 comments on commit 9b5f863

Please sign in to comment.