Apollo client ignoring argument for GQL REST query

76 Views Asked by At

I have a query that is defined like this:

    export const getProjectCosts = gql`
        query GetProjectCosts($projectId: Int) {
            ProjectCostList @rest(type: "ProjectCostList", path: "ProjectCosts/{args.projectId}") {
                id
                projectId
                cost
                description
                costType
                budgetYear
                createdByUser
                createdDate
            }
        }
    `;
    
    export const useGetProjectCostsListQuery = (baseOptions?: QueryHookOptions<ProjectCostList>) => {
        const options = { ...baseOptions };
        return useQuery<ProjectCostList>(getProjectCosts, options);
    };

I call it like this:

    const {
        loading: projectCostLoading,
        error: projectCostError,
        data: projectCostData,
    } = useGetProjectCostsListQuery({
        variables: {
            projectId: args.defaultValues.id,
        },
    });

I have verified in the debugger that the argument is sent correctly. However, in the browser I see a warning:

Warning: RestLink caught an error while unpacking ProjectCosts/{args.projectId}|args.projectId This tends to happen if you forgot to pass a parameter needed for creating an @rest(path, or if RestLink was configured to deeply unpack a path parameter that wasn't provided. This message will only log once per detected instance. Trouble-shooting hint: check @rest(path: and the variables provided to this query. (anonymous) @ restLink.ts:567

And a request is sent to api/ProjectCosts/ instead of api/ProjectCosts/1234, which not surprisingly fails with a HTTP 404 error.

Is this a bug or am I doing something wrong? I found this issue on Github, which looks very similar to what I'm experiencing.

We're using a custom .NET backend. If it's a bug, is there a workaround?

1

There are 1 best solutions below

0
TravelingFox On

The problem was that the parameter has to be declared twice. This is the solution:

export const getProjectCosts = gql`
    query GetProjectCosts($projectId: Int!) {
        ProjectCostList(projectId: $projectId) @rest(type: "ProjectCostList", path: "ProjectCosts/{args.projectId}") {