How to use RTK Query properly when I have different basequeries? Ex. axiosBaseQuery and graphqlBaseQuery

Viewed 11

My understanding is there should only be one createapi in your app. How would I use two basequeries in RTK query?

Ex. createApi with axiosBaseQuery

export const apiSlice = createApi({
 reducerPath: "apiSlice",
 baseQuery: axiosBaseQuery,
 endpoints: (builder) => ({
   fetchSomething: builder.query<Resource, string>({
     query: (resourceUUID) => ({
       url: `/services/buresource/collections/${resourceUUID}?component_level=1`,
       method: "get",
      }),
    }),
  }),
});

Ex. createapi with graphql basequery

export const api = createApi({
  baseQuery: graphqlBaseQuery({
    baseUrl: 'https://graphqlzero.almansi.me/api',
  }),
  endpoints: (builder) => ({
    getPosts: builder.query({
      query: () => ({
        body: gql`
          query {
            posts {
              data {
                id
                title
              }
            }
          }
        `,
      }),
      transformResponse: (response) => response.posts.data,
    }),
  }),
})

I'm pretty new to rtk-query and react native in general.

1 Answers

If you are really talking to different apis (and I imagine it couldnt' get more different if one uses graphql and the other REST), it is also okay to have multiple createApis. You just shouldn't do that for one interconnected dataset.

Just make sure to set the reducerPath option to createApi when creating those.

Related