Skip to content

Commit

Permalink
fix(core): add all objects in resolver.of to context.types
Browse files Browse the repository at this point in the history
  • Loading branch information
xcfox committed Nov 19, 2024
1 parent c0b724e commit 2694a12
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## next (YYYY-MM-DD)

- Fix: add all objects in `resolver.of` to `context.types`

## 0.4.0 (2024-11-120)

- Refactor: follow standard-schema ([#7](https://github.com/modevol-com/gqloom/pull/7))
Expand Down
15 changes: 9 additions & 6 deletions packages/core/src/resolver/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
lexicographicSortSchema,
printSchema,
} from "graphql"
import { describe, expect, it } from "vitest"
Expand Down Expand Up @@ -148,14 +149,16 @@ describe("resolver", () => {
heightInMeters: field.hidden,
})
const schema = weave(r1)
expect(printSchema(schema)).toMatchInlineSnapshot(`
"type Query {
hello: Giraffe
expect(
printSchema(lexicographicSortSchema(schema))
).toMatchInlineSnapshot(`
"type Giraffe {
birthday: String!
name: String!
}
type Giraffe {
name: String!
birthday: String!
type Query {
hello: Giraffe
}"
`)
})
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/schema/schema-weaver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ describe("SchemaWeaver", () => {
name: String!
}"
`)

const giraffeResolver = resolver.of(Giraffe, {})
const schema2 = weave(giraffeResolver)
expect(
printSchema(lexicographicSortSchema(schema2))
).toMatchInlineSnapshot(`
"type Giraffe {
name: String!
}"
`)
})

it("should avoid duplicate name", () => {
Expand Down Expand Up @@ -269,15 +279,15 @@ describe("SchemaWeaver", () => {

const schema = new SchemaWeaver().add(r1).add(r2).weaveGraphQLSchema()
expect(printSchema(schema)).toMatchInlineSnapshot(`
"type Query {
dog: Dog
}
type Dog {
"type Dog {
name: String!
birthday: String!
age: Int
greeting: String
}
type Query {
dog: Dog
}"
`)
})
Expand Down
27 changes: 14 additions & 13 deletions packages/core/src/schema/schema-weaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SchemaWeaver {
public query?: LoomObjectType
public mutation?: LoomObjectType
public subscription?: LoomObjectType
public types?: GraphQLNamedType[] | null
public types: Set<GraphQLNamedType>

public context: WeaverContext

Expand All @@ -68,7 +68,7 @@ export class SchemaWeaver {
if (query != null) this.query = query
if (mutation != null) this.mutation = mutation
if (subscription != null) this.subscription = subscription
if (types != null) this.types = types.slice()
this.types = new Set(types ?? [])
this.context = context ?? initWeaverContext()
}

Expand Down Expand Up @@ -108,8 +108,7 @@ export class SchemaWeaver {
`${(gqlType as any)?.name ?? gqlType.toString()} is not a named type`
)
}, this.context)
this.types ??= []
this.types.push(gqlType)
this.types.add(gqlType)
return this
}

Expand Down Expand Up @@ -142,16 +141,18 @@ export class SchemaWeaver {

if (isNonNullType(gqlType)) gqlType = gqlType.ofType

if (isObjectType(gqlType)) {
const existing = this.context.loomObjectMap.get(gqlType)
if (existing != null) return existing
const extraObject = new LoomObjectType(gqlType, this.fieldOptions)
this.context.loomObjectMap.set(gqlType, extraObject)
return extraObject
if (!isObjectType(gqlType)) {
throw new Error(
`${(gqlType as any)?.name ?? gqlType.toString()} is not an object type`
)
}
throw new Error(
`${(gqlType as any)?.name ?? gqlType.toString()} is not an object type`
)

const existing = this.context.loomObjectMap.get(gqlType)
if (existing != null) return existing
const extraObject = new LoomObjectType(gqlType, this.fieldOptions)
this.context.loomObjectMap.set(gqlType, extraObject)
this.types.add(extraObject)
return extraObject
})()

if (resolverOptions?.extensions && parentObject)
Expand Down

0 comments on commit 2694a12

Please sign in to comment.