Celebrate Excellence in Education: Nominate Outstanding Educators by April 15!
Found this content helpful? Log in or sign up to leave a like!
Hello Canvas Community,
I am transitioning from using the Canvas REST API to the GraphQL API and need some help with a specific functionality. In the REST API, I use the endpoint "GET /api/v1/accounts/:account_id/users" with "account_id" set to "self" to refer to the domain root account. This is helpful for fetching list of of users associated with this account.
I am looking for an equivalent way to use "self" or a similar construct in the GraphQL API to achieve the same result. My goal is to query users associated with this account without explicitly specifying the account ID.
Has anyone worked with similar queries in the GraphQL API, or can someone point me in the right direction? Any examples or guidance would be greatly appreciated!
Thank you
I have not found any support for "self" with GraphQL.
I have researched this looking for a way to accomplish this. I recently gave up and just added my account ID to a configuration file I have so it's available to my code.
I tried "self", "Self", and "SELF" as the id, "self" as the "sis_id". I even tried a empty ID of "". It accepts those, but returns null for the account. The GraphQL id is a base-64 encoded version of "Account-xxx" where xxx is your account number. I tried encoding "Account-self" and using it and it didn't work, either. I even tried it with the legacy nodes. Trying "@self", "#self", and "!self" all threw invalid input errors.
I wouldn't expect most of those variations to work. GraphQL names are case-insensitive and special symbols are used as punctuators. The ID! type, which is what the id property is, is defined as a string but expects a number. The documentation (within the graphiql interface) says "The ID scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as "4") or integer (such as 4) input value will be accepted as an ID." If "self" were acceptable, then it wouldn't be cacheable (caching appears to be optional).
I looked for alternatives to "self" such as "default." Trying an id of "default" gives an invalid input error.
Trying to leave off the id completely gets you the message that you must specify exactly one of id or sisId.
I looked through the Canvas source code related to GraphQL and didn't find anything related to "self" as an ID. I think these would be handled by the APIs that return the data rather than GraphQL.
I'm not an expert on GraphQL, so it might be possible to add this, but I don't think it's there right now.
I suspect there may be some limitation of GraphQL or the GraphQL library that Canvas is using that makes this non-trivial. For instance, the library may say that anything that looks like a number should be treated as a number and any non-numeric string should be considered as the base-64 encoded ID. To allow recognition of "self" might mean modifying the library and then maintaining those changes rather than just using the base library.
To participate in the Instructure Community, you need to sign up or log in:
Sign In