Skip to content

Commit

Permalink
feat: allow multiple queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenie committed Mar 18, 2022
1 parent 5f071b3 commit ff5b5e0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
# Vue Contentful Hook

A hook to call the contentful API
A hook to call the contentful API using GraphQl

## Usage

Ideally you should pass env variables as token and spaceId

```ts
export interface Dummy {
readonly items: {
readonly name: string;
export interface Dummy<T, U> {
readonly dummyCollection: {
readonly total: number;
readonly skip: number;
readonly limit: number;
readonly items: readonly T[];
};

readonly someOtherDummyCollection: {
readonly total: number;
readonly skip: number;
readonly limit: number;
readonly items: readonly U[];
};
}

Expand All @@ -20,14 +30,22 @@ const query = `
name
}
}
someOtherDummyCollection {
items {
name
}
}
}
`;
const { data } = useContentful<Dummy>(query, {
const { data } = useContentful<Dummy<string, number>>(query, {
token: "myToken",
spaceId: "mySpaceId",
});

console.log("data", data.value?.items);
// an array of strings
console.log("data", data.value?.dummyCollection.items);
// an array of numbers
console.log("data", data.value?.dummyCollection.items);
```

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
Expand Down
8 changes: 4 additions & 4 deletions lib/use-contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ type ContentfulOptions = {
}

type ContentfulResponse<T> = {
readonly data: Readonly<Record<string, Readonly<T>>>
readonly data: Readonly<T>
readonly errors: readonly string[]
}

export const useContentful = <T>(
query: string,
{ spaceId, token }: ContentfulOptions,
) => {
const data = ref<Readonly<T>>()
const errors = ref<readonly string[]>()
const data = ref<ContentfulResponse<T>['data']>()
const errors = ref<ContentfulResponse<T>['errors']>()
const isLoading = ref<boolean>(true)

const URI = `https://graphql.contentful.com/content/v1/spaces/${spaceId}`
Expand All @@ -35,7 +35,7 @@ export const useContentful = <T>(
isLoading.value = false

if (response) {
data.value = response[Object.keys(response)[0]]
data.value = response
}

if (contenfulErrors) {
Expand Down

0 comments on commit ff5b5e0

Please sign in to comment.