-
Notifications
You must be signed in to change notification settings - Fork 347
Supporting variables in GraphQL queries and mutations #290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a30f0d7
72c8c9e
7b8ac7f
cd1837d
db5cf5e
30ff83f
3eed8ad
6c29896
8b324cd
5140666
6c581b5
110dba6
5c7ed86
38f347f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,39 @@ public static void Init(TestContext context) | |
| { | ||
| _clientProvider = new CosmosClientProvider(TestHelper.DataGatewayConfig); | ||
| _metadataStoreProvider = new MetadataStoreProviderForTest(); | ||
| string jsonString = File.ReadAllText("schema.gql"); | ||
| string jsonString = @" | ||
| type Query { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same any reason for defining it inline?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It reduces a reliance on external files. I've been testing with different GraphQL schemas but in doing so the tests will start failing as they are using the same file that the runtime uses (unless you go and create a new config). By having the schema inline it's clear looking at the tests what schema is being loaded. |
||
| characterList: [Character] | ||
| characterById (id : ID!): Character | ||
| planetById (id: ID! = 1): Planet | ||
| getPlanet(id: ID, name: String): Planet | ||
| planetList: [Planet] | ||
| planets(first: Int, after: String): PlanetConnection | ||
| } | ||
|
|
||
| type Mutation { | ||
| addPlanet(id: String, name: String): Planet | ||
| deletePlanet(id: String): Planet | ||
| } | ||
|
|
||
| type PlanetConnection { | ||
| items: [Planet] | ||
| endCursor: String | ||
| hasNextPage: Boolean | ||
| } | ||
|
|
||
| type Character { | ||
| id : ID, | ||
| name : String, | ||
| type: String, | ||
| homePlanet: Int, | ||
| primaryFunction: String | ||
| } | ||
|
|
||
| type Planet { | ||
| id : ID, | ||
| name : String | ||
| }"; | ||
| _metadataStoreProvider.GraphQLSchema = jsonString; | ||
| _queryEngine = new CosmosQueryEngine(_clientProvider, _metadataStoreProvider); | ||
| _mutationEngine = new CosmosMutationEngine(_clientProvider, _metadataStoreProvider); | ||
|
|
@@ -125,18 +157,27 @@ internal static void RegisterGraphQLType(string id, | |
| /// Executes the GraphQL request and returns the results | ||
| /// </summary> | ||
| /// <param name="queryName"> Name of the GraphQL query/mutation</param> | ||
| /// <param name="graphQLQuery"> The GraphQL query/mutation</param> | ||
| /// <param name="query"> The GraphQL query/mutation</param> | ||
| /// <param name="variables">Variables to be included in the GraphQL request. If null, no variables property is included in the request, to pass an empty object provide an empty dictionary</param> | ||
| /// <returns></returns> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add the param |
||
| internal static async Task<JsonElement> ExecuteGraphQLRequestAsync(string queryName, string graphQLQuery) | ||
| internal static async Task<JsonElement> ExecuteGraphQLRequestAsync(string queryName, string query, Dictionary<string, object> variables = null) | ||
| { | ||
| string queryJson = JObject.FromObject(new | ||
| { | ||
| query = graphQLQuery | ||
| }).ToString(); | ||
| string queryJson = variables == null ? | ||
| JObject.FromObject(new { query }).ToString() : | ||
| JObject.FromObject(new | ||
| { | ||
| query, | ||
| variables | ||
| }).ToString(); | ||
| _controller.ControllerContext.HttpContext = GetHttpContextWithBody(queryJson); | ||
| JsonElement graphQLResult = await _controller.PostAsync(); | ||
|
|
||
| if (graphQLResult.TryGetProperty("errors", out JsonElement errors)) | ||
| { | ||
| Assert.Fail(errors.GetRawText()); | ||
| } | ||
|
|
||
| return graphQLResult.GetProperty("data").GetProperty(queryName); | ||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any functional reason to define this inline? Better readability? instead of using PlanetConnectionQueryStringFormat?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find string interpolation more readable than using
string.Format. After all, it's really just doingstring.Formatwith syntactic sugar.