Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Fixed a bug in #221 + updated yoga template
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Nov 1, 2018
1 parent 5189447 commit eabd13d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 118 deletions.
162 changes: 81 additions & 81 deletions packages/graphqlgen-templates/yoga/src/generated/graphqlgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,86 +57,6 @@ export namespace QueryResolvers {
}
}

export namespace MutationResolvers {
export const defaultResolvers = {}

export interface ArgsCreateuser {
name: string | null
}

export interface ArgsCreatedraft {
title: string
content: string
authorId: string
}

export interface ArgsDeletepost {
id: string
}

export interface ArgsPublish {
id: string
}

export type CreateuserResolver = (
parent: {},
args: ArgsCreateuser,
ctx: Context,
info: GraphQLResolveInfo,
) => User | Promise<User>

export type CreatedraftResolver = (
parent: {},
args: ArgsCreatedraft,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | Promise<Post>

export type DeletepostResolver = (
parent: {},
args: ArgsDeletepost,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

export type PublishResolver = (
parent: {},
args: ArgsPublish,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

export interface Type {
createUser: (
parent: {},
args: ArgsCreateuser,
ctx: Context,
info: GraphQLResolveInfo,
) => User | Promise<User>

createDraft: (
parent: {},
args: ArgsCreatedraft,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | Promise<Post>

deletePost: (
parent: {},
args: ArgsDeletepost,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

publish: (
parent: {},
args: ArgsPublish,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>
}
}

export namespace PostResolvers {
export const defaultResolvers = {
id: (parent: Post) => parent.id,
Expand Down Expand Up @@ -269,9 +189,89 @@ export namespace UserResolvers {
}
}

export namespace MutationResolvers {
export const defaultResolvers = {}

export interface ArgsCreateUser {
name: string | null
}

export interface ArgsCreateDraft {
title: string
content: string
authorId: string
}

export interface ArgsDeletePost {
id: string
}

export interface ArgsPublish {
id: string
}

export type CreateUserResolver = (
parent: {},
args: ArgsCreateUser,
ctx: Context,
info: GraphQLResolveInfo,
) => User | Promise<User>

export type CreateDraftResolver = (
parent: {},
args: ArgsCreateDraft,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | Promise<Post>

export type DeletePostResolver = (
parent: {},
args: ArgsDeletePost,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

export type PublishResolver = (
parent: {},
args: ArgsPublish,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

export interface Type {
createUser: (
parent: {},
args: ArgsCreateUser,
ctx: Context,
info: GraphQLResolveInfo,
) => User | Promise<User>

createDraft: (
parent: {},
args: ArgsCreateDraft,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | Promise<Post>

deletePost: (
parent: {},
args: ArgsDeletePost,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>

publish: (
parent: {},
args: ArgsPublish,
ctx: Context,
info: GraphQLResolveInfo,
) => Post | null | Promise<Post | null>
}
}

export interface Resolvers {
Query: QueryResolvers.Type
Mutation: MutationResolvers.Type
Post: PostResolvers.Type
User: UserResolvers.Type
Mutation: MutationResolvers.Type
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import { Resolvers } from '../graphqlgen'

import { Query } from './Query'
import { Mutation } from './Mutation'
import { Post } from './Post'
import { User } from './User'
import { Mutation } from './Mutation'

export const resolvers: Resolvers = {
Query,
Mutation,
Post,
User,
Mutation,
}
2 changes: 1 addition & 1 deletion packages/graphqlgen-templates/yoga/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ type User {
id: ID!
name: String
posts: [Post!]!
}
}
106 changes: 72 additions & 34 deletions packages/graphqlgen/src/source-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import {

/** Our own GraphQL schema/types abstraction. */
export type GraphQLTypes = {
types: GraphQLTypeObject[],
unions: GraphQLUnionObject[],
enums: GraphQLEnumObject[],
types: GraphQLTypeObject[]
unions: GraphQLUnionObject[]
enums: GraphQLEnumObject[]
}

/** Converts typeDefs, e.g. the raw SDL string, into our `GraphQLTypes`. */
export function extractTypes(typeDefs: string): GraphQLTypes {
const schema = buildASTSchema(parse(typeDefs));
const schema = buildASTSchema(parse(typeDefs))
const types = extractGraphQLTypes(schema)
const unions = extractGraphQLUnions(schema)
const enums = extractGraphQLEnums(schema)
Expand Down Expand Up @@ -78,6 +78,12 @@ export type GraphQLUnionObject = {
types: GraphQLTypeDefinition[]
}

interface FinalType {
isRequired: boolean
isArray: boolean
type: GraphQLInputType | GraphQLOutputType
}

export const GraphQLScalarTypeArray = [
'Boolean',
'Int',
Expand Down Expand Up @@ -120,27 +126,51 @@ function extractTypeDefinition(
return typeLike
}

const getFinalType = (
type: GraphQLInputType | GraphQLOutputType,
acc: FinalType = { isArray: false, isRequired: false, type },
): FinalType => {
if (type instanceof GraphQLNonNull) {
acc.isRequired = true
}
if (type instanceof GraphQLList) {
acc.isArray = true
}

if (type instanceof GraphQLNonNull || type instanceof GraphQLList) {
return getFinalType(
(type as GraphQLNonNull<any> | GraphQLList<any>).ofType,
acc,
)
}

return {
...acc,
type,
}
}

function extractTypeLike(
schema: GraphQLSchema,
type: GraphQLInputType | GraphQLOutputType,
): GraphQLType {
const typeLike: GraphQLType = {} as GraphQLType
if (type instanceof GraphQLNonNull) {
const { isArray, isRequired, type: finalType } = getFinalType(type)
if (isRequired) {
typeLike.isRequired = true
type = (type as GraphQLNonNull<any>).ofType
}
if (type instanceof GraphQLList) {
if (isArray) {
typeLike.isArray = true
type = (type as GraphQLList<any>).ofType
}
if (type instanceof GraphQLObjectType
|| type instanceof GraphQLInterfaceType
|| type instanceof GraphQLEnumType
|| type instanceof GraphQLUnionType
|| type instanceof GraphQLInputObjectType
|| type instanceof GraphQLScalarType
if (
finalType instanceof GraphQLObjectType ||
finalType instanceof GraphQLInterfaceType ||
finalType instanceof GraphQLEnumType ||
finalType instanceof GraphQLUnionType ||
finalType instanceof GraphQLInputObjectType ||
finalType instanceof GraphQLScalarType
) {
typeLike.name = type.name
typeLike.name = finalType.name
}
const typeDefinitionLike = extractTypeDefinition(schema, typeLike)
return {
Expand All @@ -149,27 +179,35 @@ function extractTypeLike(
}
}

function extractTypeFieldsFromObjectType(schema: GraphQLSchema, node: GraphQLObjectType) {
function extractTypeFieldsFromObjectType(
schema: GraphQLSchema,
node: GraphQLObjectType,
) {
const fields: GraphQLTypeField[] = []
Object.values(node.getFields()).forEach((fieldNode: GraphQLField<any, any>) => {
const fieldType: GraphQLType = extractTypeLike(schema, fieldNode.type)
const fieldArguments: GraphQLTypeArgument[] = []
fieldNode.args.forEach((arg: GraphQLArgument) => {
fieldArguments.push({
name: arg.name,
type: extractTypeLike(schema, arg.type),
Object.values(node.getFields()).forEach(
(fieldNode: GraphQLField<any, any>) => {
const fieldType: GraphQLType = extractTypeLike(schema, fieldNode.type)
const fieldArguments: GraphQLTypeArgument[] = []
fieldNode.args.forEach((arg: GraphQLArgument) => {
fieldArguments.push({
name: arg.name,
type: extractTypeLike(schema, arg.type),
})
})
})
fields.push({
name: fieldNode.name,
type: fieldType,
arguments: fieldArguments,
})
})
fields.push({
name: fieldNode.name,
type: fieldType,
arguments: fieldArguments,
})
},
)
return fields
}

function extractTypeFieldsFromInputType(schema: GraphQLSchema, node: GraphQLInputObjectType) {
function extractTypeFieldsFromInputType(
schema: GraphQLSchema,
node: GraphQLInputObjectType,
) {
const fields: GraphQLTypeField[] = []
Object.values(node.getFields()).forEach((input: GraphQLInputField) => {
fields.push({
Expand All @@ -185,8 +223,8 @@ function extractGraphQLTypes(schema: GraphQLSchema) {
const types: GraphQLTypeObject[] = []
Object.values(schema.getTypeMap()).forEach((node: GraphQLNamedType) => {
// Ignore meta types like __Schema and __TypeKind
if (node.name.startsWith("__")) {
return;
if (node.name.startsWith('__')) {
return
}
if (node instanceof GraphQLEnumType) {
types.push({
Expand All @@ -200,7 +238,7 @@ function extractGraphQLTypes(schema: GraphQLSchema) {
isScalar: false,
isInterface: false,
},
fields: [] // extractTypeFields(schema, node),
fields: [], // extractTypeFields(schema, node),
})
} else if (node instanceof GraphQLObjectType) {
types.push({
Expand Down

0 comments on commit eabd13d

Please sign in to comment.