Skip to content

Commit

Permalink
Test context ordering/duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
captbaritone committed Aug 11, 2024
1 parent 9571cbe commit aeb93c1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/tests/fixtures/resolver_context/ContextValueBeforeArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @gqlType */
export class SomeType {
/** @gqlField */
greeting(ctx: SomeOtherType, args: { fallbackGreeting: string }): string {
return ctx.greeting ?? args.fallbackGreeting;
}
}

/** @gqlContext */
type SomeOtherType = { greeting?: string };
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-----------------
INPUT
-----------------
/** @gqlType */
export class SomeType {
/** @gqlField */
greeting(ctx: SomeOtherType, args: { fallbackGreeting: string }): string {
return ctx.greeting ?? args.fallbackGreeting;
}
}

/** @gqlContext */
type SomeOtherType = { greeting?: string };

-----------------
OUTPUT
-----------------
-- SDL --
type SomeType {
greeting(fallbackGreeting: String!): String @metadata
}
-- TypeScript --
import { GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLNonNull } from "graphql";
export function getSchema(): GraphQLSchema {
const SomeTypeType: GraphQLObjectType = new GraphQLObjectType({
name: "SomeType",
fields() {
return {
greeting: {
name: "greeting",
type: GraphQLString,
args: {
fallbackGreeting: {
name: "fallbackGreeting",
type: new GraphQLNonNull(GraphQLString)
}
},
resolve(source, args, context) {
return source.greeting(context, args);
}
}
};
}
});
return new GraphQLSchema({
types: [SomeTypeType]
});
}
12 changes: 12 additions & 0 deletions src/tests/fixtures/resolver_context/ContextValueReadTwice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// No valid reason to do this, but just asserting that it works, since it happens to.

/** @gqlType */
export class SomeType {
/** @gqlField */
greeting(ctx: SomeOtherType, alsoContext: SomeOtherType): string {
return ctx.greeting ?? "Hello, world!";
}
}

/** @gqlContext */
type SomeOtherType = { greeting?: string };
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-----------------
INPUT
-----------------
// No valid reason to do this, but just asserting that it works, since it happens to.

/** @gqlType */
export class SomeType {
/** @gqlField */
greeting(ctx: SomeOtherType, alsoContext: SomeOtherType): string {
return ctx.greeting ?? "Hello, world!";
}
}

/** @gqlContext */
type SomeOtherType = { greeting?: string };

-----------------
OUTPUT
-----------------
-- SDL --
type SomeType {
greeting: String @metadata
}
-- TypeScript --
import { GraphQLSchema, GraphQLObjectType, GraphQLString } from "graphql";
export function getSchema(): GraphQLSchema {
const SomeTypeType: GraphQLObjectType = new GraphQLObjectType({
name: "SomeType",
fields() {
return {
greeting: {
name: "greeting",
type: GraphQLString,
resolve(source, _args, context) {
return source.greeting(context, context);
}
}
};
}
});
return new GraphQLSchema({
types: [SomeTypeType]
});
}

0 comments on commit aeb93c1

Please sign in to comment.