func New()

in graphql/executor/testexecutor/testexecutor.go [41:127]


func New() *TestExecutor {
	next := 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!
    }
  `})

	exec := &TestExecutor{
		next: next,
	}

	exec.schema = &graphql.ExecutableSchemaMock{
		ExecFunc: func(ctx context.Context) graphql.ResponseHandler {
			opCtx := graphql.GetOperationContext(ctx)
			switch opCtx.Operation.Operation {
			case ast.Query:
				ran := false
				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"),
							},
						},
					})
					data := graphql.GetOperationContext(ctx).RootResolverMiddleware(ctx, func(ctx context.Context) graphql.Marshaler {
						res, err := graphql.GetOperationContext(ctx).ResolverMiddleware(ctx, func(ctx context.Context) (any, error) {
							// return &graphql.Response{Data: []byte(`{"name":"test"}`)}, nil
							return &MockResponse{Name: "test"}, nil
						})
						if err != nil {
							panic(err)
						}

						return res.(*MockResponse)
					})

					var buf bytes.Buffer
					data.MarshalGQL(&buf)

					return &graphql.Response{Data: buf.Bytes()}
				}
			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"}`),
						}
					}
				}
			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 exec.complexity, true
		},
	}

	exec.Executor = executor.New(exec.schema)
	return exec
}