litmuschaos / litmus

Litmus helps SREs and developers practice chaos engineering in a Cloud-native way. Chaos experiments are published at the ChaosHub (https://hub.litmuschaos.io). Community notes is at https://hackmd.io/a4Zu_sH4TZGeih-xCimi3Q
https://litmuschaos.io
Apache License 2.0
4.44k stars 697 forks source link

Converted an extended operation type to a root type in the GraphQL schema #4894

Closed suyeon-jung-dev closed 1 month ago

suyeon-jung-dev commented 2 months ago

Proposed changes

I converted an extended operation type to a root type in the GraphQL schema for the following 3 reasons:

  1. Initially, this PR included root Query, Mutations, and Subscriptions. See PR #3602.
  2. According to the GraphQL specification, all three operations require root operations to fulfill our intended purposes. See GraphQL specification

The query root operation type must be provided and must be an Object type.

The mutation root operation type is optional; if it is not provided, the service does not support mutations. If it is provided, it must be an Object type.

Similarly, the subscription root operation type is also optional; if it is not provided, the service does not support subscriptions. If it is provided, it must be an Object type.

According to the GraphQL specification, a schema must define the root operation types for each kind of operation it supports: queries, mutations, and subscriptions. This implies that there must be a clear starting point for executing any operation in the schema.

  1. Also. Without root types, there would be no structured way to initiate queries or mutations, this graphql server could lead to the GraphQL server rendering the schema ineffectively.

I believe this change will ensure that our schema adheres to the GraphQL specifications, potentially improving compatibility and maintainability.

Resolve #4893

Types of changes

What types of changes does your code introduce to Litmus? Put an x in the boxes that apply

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Dependency

Special notes for your reviewer:

Saranya-jena commented 2 months ago

@suyeon-jung-dev can you please confirm if you have tried running the gqlgen command and it's running fine? go run github.com/99designs/gqlgen generate

suyeon-jung-dev commented 2 months ago

@suyeon-jung-dev can you please confirm if you have tried running the gqlgen command and it's running fine? go run github.com/99designs/gqlgen generate

Hi @Saranya-jena

I refactored root types to Query and Mutation in chaos_experiment.graphqls to ensure no changes during gqlgen build.

I've attached the gqlgen generate results below.

image

And, I'll include the changes in generated.go.

image
suyeon-jung-dev commented 1 month ago

I added generated.go file as well.

And graphql server works well. I attached some logs from graphql server below.

{"body_size":116,"client_ip":"::1","error":"","file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/api/middleware/logger.go:60","func":"main.setupGin.DefaultStructuredLogger.StructuredLogger.func2","latency":"16.802833ms","level":"info","method":"POST","msg":"","path":"/query","status":200,"time":"2024-09-20T21:00:03+09:00"}
{"body_size":90,"client_ip":"::1","error":"","file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/api/middleware/logger.go:60","func":"main.setupGin.DefaultStructuredLogger.StructuredLogger.func2","latency":"16.807667ms","level":"info","method":"POST","msg":"","path":"/query","status":200,"time":"2024-09-20T21:00:03+09:00"}
{"file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/graph/environment.resolvers.go:20","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph.(*mutationResolver).CreateEnvironment","level":"info","msg":"request received to create new environment","projectId":"ef7f1c15-933c-4e2e-a44a-25d62da07649","time":"2024-09-20T21:00:27+09:00"}
{"body_size":259,"client_ip":"::1","error":"","file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/api/middleware/logger.go:60","func":"main.setupGin.DefaultStructuredLogger.StructuredLogger.func2","latency":"27.907791ms","level":"info","method":"POST","msg":"","path":"/query","status":200,"time":"2024-09-20T21:00:27+09:00"}
{"file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/graph/environment.resolvers.go:104","func":"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph.(*queryResolver).ListEnvironments","level":"info","msg":"request received to list environments","projectId":"ef7f1c15-933c-4e2e-a44a-25d62da07649","time":"2024-09-20T21:00:27+09:00"}
{"body_size":458,"client_ip":"::1","error":"","file":"/Users/jeongsuyeon/IdeaProjects/suyeon-jung-dev/litmus/chaoscenter/graphql/server/api/middleware/logger.go:60","func":"main.setupGin.DefaultStructuredLogger.StructuredLogger.func2","latency":"21.589334ms","level":"info","method":"POST","msg":"","path":"/query","status":200,"time":"2024-09-20T21:00:27+09:00"}
Saranya-jena commented 1 month ago

Awesome thanks for confirming! 🚀