func New()

in graphql/handler/testserver/testserver.go [19:125]


func New() *TestServer {
	next := make(chan struct{})
	completeSubscription := make(chan struct{})

	schema := gqlparser.MustLoadSchema(&ast.Source{Input: `
		type Query {
			name: String!
			find(id: Int!): String!
		}
		type Mutation {
			name: String!
		}
		type Subscription {
			name: String!
		}
	`})

	srv := &TestServer{
		next:                 next,
		completeSubscription: completeSubscription,
	}

	srv.Server = handler.New(&graphql.ExecutableSchemaMock{
		ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
			opCtx := graphql.GetOperationContext(ctx)
			switch opCtx.Operation.Operation {
			case ast.Query:
				ran := false
				// If the query contains @defer, we will mimic a deferred response.
				if strings.Contains(opCtx.RawQuery, "@defer") {
					initialResponse := true
					return func(context context.Context) *graphql.Response {
						select {
						case <-ctx.Done():
							return nil
						case <-next:
							if initialResponse {
								initialResponse = false
								hasNext := true
								return &graphql.Response{
									Data:    []byte(`{"name":null}`),
									HasNext: &hasNext,
								}
							}
							hasNext := false
							return &graphql.Response{
								Data:    []byte(`{"name":"test"}`),
								HasNext: &hasNext,
							}
						case <-completeSubscription:
							return nil
						}
					}
				}
				return func(ctx context.Context) *graphql.Response {
					if ran {
						return nil
					}
					ran = true
					// Field execution happens inside the generated code, lets simulate some of it.
					ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
						Object: "Query",
						Field: graphql.CollectedField{
							Field: &ast.Field{
								Name:       "name",
								Alias:      "name",
								Definition: schema.Types["Query"].Fields.ForName("name"),
							},
						},
					})
					res, err := graphql.GetOperationContext(ctx).
						ResolverMiddleware(ctx, func(ctx context.Context) (any, error) {
							return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
						})
					if err != nil {
						panic(err)
					}
					return res.(*graphql.Response)
				}
			case ast.Mutation:
				return graphql.OneShot(graphql.ErrorResponse(ctx, "mutations are not supported"))
			case ast.Subscription:
				return func(context context.Context) *graphql.Response {
					select {
					case <-ctx.Done():
						return nil
					case <-next:
						return &graphql.Response{
							Data: []byte(`{"name":"test"}`),
						}
					case <-completeSubscription:
						return nil
					}
				}
			default:
				return graphql.OneShot(graphql.ErrorResponse(ctx, "unsupported GraphQL operation"))
			}
		},
		SchemaFunc: func() *ast.Schema {
			return schema
		},
		ComplexityFunc: func(typeName string, fieldName string, childComplexity int, args map[string]any) (i int, b bool) {
			return srv.complexity, true
		},
	})
	return srv
}