Skip to content

Cosmos GraphQL schema should enforce the id field be non-null #1238

Open
@aaronpowell

Description

@aaronpowell

When creating a GraphQL schema to use with Cosmos, it is possible to define a nullable id field, which will then result in a runtime error trying to use the create mutation, since the id field is required for both the id and partition key.

Our tests are showing this as an incorrect pattern:

type Character @model(name:"Character") {
    id : ID,
    name : String,
    type: String,
    homePlanet: Int,
    primaryFunction: String,
    star: Star
}

type Planet @model(name:"Planet") {
    id : ID,
    name : String,
    character: Character,
    age : Int,
    dimension : String,
    stars: [Star]
    tags: [String!]
}

type Star @model(name:"StarAlias") {
    id : ID,
    name : String
}

There is a test that covers the error you would get at runtime:

[TestMethod]
public async Task MutationMissingRequiredIdReturnError()
{
// Run mutation Add planet without id
const string name = "test_name";
string mutation = $@"
mutation {{
createPlanet (item: {{ name: ""{name}"" }}) {{
id
name
}}
}}";
JsonElement response = await ExecuteGraphQLRequestAsync("createPlanet", mutation, variables: new());
Assert.IsTrue(response[0].GetProperty("message").ToString().Contains("The input content is invalid because the required properties - 'id; ' - are missing"));
}

Since this is a requirement, it should be made clear in the GraphQL schema that is served by DAB, to ensure that clients are aware of what the contract is with the server, rather than waiting until runtime to receive an error response.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions