improve: graphql query builder test (#7618)

This commit is contained in:
Pooja
2026-04-01 17:11:22 +05:30
committed by GitHub
parent 652f3cc3fe
commit 28d1ba2438
3 changed files with 177 additions and 95 deletions

View File

@@ -5,12 +5,34 @@ const setupGraphQL = async (app) => {
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type User {
id: ID!
name: String!
email: String!
age: Int
posts: [Post!]!
}
type Post {
id: ID!
title: String!
body: String!
author: User!
comments: [Comment!]!
}
type Comment {
id: ID!
text: String!
author: User!
}
type Company {
ceo: String
name: String
founder: String
}
input ICreate {
id: String!
}
@@ -19,12 +41,40 @@ const setupGraphQL = async (app) => {
success: Boolean
}
union SearchResult = User | Post
input CreateUserInput {
name: String!
email: String!
age: Int
}
input UpdateUserInput {
name: String
email: String
age: Int
}
input CreatePostInput {
title: String!
body: String!
authorId: ID!
}
type Query {
company: Company
user(id: ID!): User
users(limit: Int, offset: Int): [User!]!
post(id: ID!): Post
search(term: String!): [SearchResult!]!
}
type Mutation {
create(payload: ICreate!): Message
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User
deleteUser(id: ID!): Boolean!
createPost(input: CreatePostInput!): Post!
}
`,
resolvers: {
@@ -33,12 +83,54 @@ const setupGraphQL = async (app) => {
ceo: 'Elon Musk',
name: 'SpaceX',
founder: 'Elon Musk'
})
}),
user: (_parent, args) => ({
id: args.id,
name: 'John Doe',
email: 'john@example.com',
age: 30,
posts: []
}),
users: () => [
{ id: '1', name: 'John Doe', email: 'john@example.com', age: 30, posts: [] },
{ id: '2', name: 'Jane Smith', email: 'jane@example.com', age: 25, posts: [] }
],
post: (_parent, args) => ({
id: args.id,
title: 'Test Post',
body: 'Post body',
author: { id: '1', name: 'John Doe', email: 'john@example.com', age: 30, posts: [] },
comments: []
}),
search: () => []
},
Mutation: {
create: () => ({
success: true
}),
createUser: (_parent, { input }) => ({
id: '3',
...input,
posts: []
}),
updateUser: (_parent, { id, input }) => ({
id,
name: input.name || 'John Doe',
email: input.email || 'john@example.com',
age: input.age || 30,
posts: []
}),
deleteUser: () => true,
createPost: (_parent, { input }) => ({
id: '1',
title: input.title,
body: input.body,
author: { id: input.authorId, name: 'John Doe', email: 'john@example.com', age: 30, posts: [] },
comments: []
})
},
SearchResult: {
__resolveType: (obj) => (obj.email ? 'User' : 'Post')
}
}
}),