Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.07k stars 107 forks source link

Export the query-over-the-wire as a constant #236

Closed csilvers closed 1 year ago

csilvers commented 1 year ago

Generated genqlient code might look like this:

func Progress_AssignmentByID(
        ctx gqlclient.KAContext,
        client graphql.Client,
        id string,
) (*Progress_AssignmentByIDResponse, error) {
        req := &graphql.Request{
                OpName: "Progress_AssignmentByID",
                Query: `
query Progress_AssignmentByID ($id: String!) {
        user {
                assignment(id: $id) {
                        studentKaids
                        exerciseConfig {
                                itemPickerStrategy
                                assessmentItemIds
                        }
                }
        }
}
`,
                Variables: &__Progress_AssignmentByIDInput{
                        Id: id,
                },
        }
        var err error
        ...

Request.Query differs from the graphql code as written in the source because, among other things, the @genqlient directive has been removed.

For our application, we'd like to know in Go code what the actual text-over-the-wire is. We can look in generated/safelist.json, but it would be easier just to expose it in Go. I'm thinking of an API like this:

const Progress_AssignmentByIDQuery = `
query Progress_AssignmentByID ($id: String!) {
        user {
                assignment(id: $id) {
                        studentKaids
                        exerciseConfig {
                                itemPickerStrategy
                                assessmentItemIds
                        }
                }
        }
}
`

func Progress_AssignmentByID(
        ctx gqlclient.KAContext,
        client graphql.Client,
        id string,
) (*Progress_AssignmentByIDResponse, error) {
        req := &graphql.Request{
                OpName: "Progress_AssignmentByID",
                Query: Progress_AssignmentByIDQuery,
                Variables: &__Progress_AssignmentByIDInput{
                        Id: id,
                },
        }
        var err error
        ...

Is this a change you would be amenable to seeing?

benjaminjkraft commented 1 year ago

That seems fine!

(Technically the suffix should probably be Operation although every GraphQL library is wildly inconsistent about this so if you feel Query is clearer that's ok too.)