├── .eslintignore ├── .eslintrc.cjs ├── .gitignore ├── .prettierignore ├── .prettierrc ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── src ├── contexts.ts ├── index.ts ├── qwikql-component.tsx ├── types.ts ├── useHeaders.ts ├── useMutation.ts ├── useQuery.ts └── util │ └── toQwikqlError.ts ├── tsconfig.json └── vite.config.ts /.eslintignore: -------------------------------------------------------------------------------- 1 | **/*.log 2 | **/.DS_Store 3 | *. 4 | .vscode/settings.json 5 | .history 6 | .yarn 7 | bazel-* 8 | bazel-bin 9 | bazel-out 10 | bazel-qwik 11 | bazel-testlogs 12 | dist 13 | dist-dev 14 | lib 15 | lib-types 16 | etc 17 | external 18 | node_modules 19 | temp 20 | tsc-out 21 | tsdoc-metadata.json 22 | target 23 | output 24 | rollup.config.js 25 | build 26 | .cache 27 | .vscode 28 | .rollup.cache 29 | dist 30 | tsconfig.tsbuildinfo 31 | vite.config.ts 32 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | browser: true, 5 | es2021: true, 6 | node: true 7 | }, 8 | extends: [ 9 | 'eslint:recommended', 10 | 'plugin:@typescript-eslint/recommended', 11 | 'plugin:qwik/recommended' 12 | ], 13 | parser: '@typescript-eslint/parser', 14 | parserOptions: { 15 | tsconfigRootDir: __dirname, 16 | project: ['./tsconfig.json'], 17 | ecmaVersion: 2021, 18 | sourceType: 'module', 19 | ecmaFeatures: { 20 | jsx: true 21 | } 22 | }, 23 | plugins: ['@typescript-eslint'], 24 | rules: { 25 | '@typescript-eslint/no-explicit-any': 'off', 26 | '@typescript-eslint/explicit-module-boundary-types': 'off', 27 | '@typescript-eslint/no-inferrable-types': 'off', 28 | '@typescript-eslint/no-non-null-assertion': 'off', 29 | '@typescript-eslint/no-empty-interface': 'off', 30 | '@typescript-eslint/no-namespace': 'off', 31 | '@typescript-eslint/no-empty-function': 'off', 32 | '@typescript-eslint/no-this-alias': 'off', 33 | '@typescript-eslint/ban-types': 'off', 34 | '@typescript-eslint/ban-ts-comment': 'off', 35 | 'prefer-spread': 'off', 36 | 'no-case-declarations': 'off', 37 | 'no-console': 'off', 38 | '@typescript-eslint/no-unused-vars': ['error'] 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Build 2 | /dist 3 | /lib 4 | /lib-types 5 | /server 6 | 7 | # Development 8 | node_modules 9 | 10 | # Cache 11 | .cache 12 | .mf 13 | .vscode 14 | .rollup.cache 15 | tsconfig.tsbuildinfo 16 | 17 | # Logs 18 | logs 19 | *.log 20 | npm-debug.log* 21 | yarn-debug.log* 22 | yarn-error.log* 23 | pnpm-debug.log* 24 | lerna-debug.log* 25 | 26 | # Editor 27 | !.vscode/extensions.json 28 | .idea 29 | .DS_Store 30 | *.suo 31 | *.ntvs* 32 | *.njsproj 33 | *.sln 34 | *.sw? 35 | 36 | # Yarn 37 | .yarn/* 38 | !.yarn/releases 39 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | # Files Prettier should not format 2 | **/*.log 3 | **/.DS_Store 4 | *. 5 | dist 6 | node_modules 7 | README.md 8 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "none", 3 | "tabWidth": 2, 4 | "semi": false, 5 | "singleQuote": true 6 | } 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Taha Shashtari 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QwikQL 2 | 3 | A GraphQL client for Qwik framework. 4 | 5 | --- 6 | 7 | ## Installation 8 | 9 | ```bash 10 | npm install qwikql graphql graphql-request 11 | ``` 12 | 13 | To use it, wrap the root component with it, and specify the GraphQL server url using the `url` prop. 14 | 15 | ```jsx 16 | import { QwikQL } from 'qwikql' 17 | 18 | export default component$(() => { 19 | return ( 20 | 23 | 24 | // ... 25 | 26 | 27 | ) 28 | }) 29 | ``` 30 | 31 | ## Queries 32 | QwikQL provides a use hook named `useQuery(QUERY)`. It takes the query as a parameter, and it returns `{ executeQuery$ }`. 33 | 34 | `executeQuery$({ variables })` is a QRL function that takes the variables (like `{ variables: { id: 'example-id' } }`) and returns a promise with the results. 35 | 36 | The best way to fetch data in Qwik is using `` component. To utilize ``, you have to create a resource using `useResource$` hook function. 37 | 38 | Here's an example: 39 | 40 | ```jsx 41 | import { useQuery } from 'qwikql' 42 | import { gql } from 'graphql-request' 43 | 44 | export default component$(() => { 45 | const ITEM_BY_ID = gql` 46 | query itemById($itemId: ID) { 47 | itemById(itemId: $itemId) { 48 | id 49 | title 50 | } 51 | } 52 | ` 53 | 54 | const { executeQuery$ } = useQuery(ITEM_BY_ID) 55 | 56 | const item = useResource$(async () => 57 | await executeQuery$({ 58 | variables: { itemId: 'example-item-id' } 59 | }) 60 | ) 61 | 62 | return ( 63 | <> 64 | ( 67 | <>Loading Item 68 | )} 69 | onResolved={(data: any) => ( 70 | <>{ data.itemById.title } 71 | )} 72 | onRejected={(error) => ( 73 | <>Error fetching item: {error} 74 | )} 75 | /> 76 | 77 | ) 78 | }) 79 | ``` 80 | 81 | ## Refetching 82 | 83 | Since we are using `useResource$` for fetching the data, we just need to retrigger it when we want to refetch the data. 84 | 85 | `useResource$` provides us with `track` function that watches a specific property in a store, and when that property is updated, then `useResource$` is called again. 86 | 87 | So, we can use this feature refetch data when the query variables (or any state we want) change. 88 | 89 | ```jsx 90 | import { useQuery } from 'qwikql' 91 | import { ITEM_BY_ID } from '~/graphql/queries' 92 | import { useStore } from '@builder.io/qwik' 93 | 94 | export default component$(() => { 95 | const itemId = useStore({ 96 | value: 'example-item-id' 97 | }) 98 | const { executeQuery$ } = useQuery(ITEM_BY_ID) 99 | 100 | const item = useResource$(async ({ track }) => { 101 | track(itemId, 'value') 102 | return await executeQuery$({ 103 | variables: { itemId: itemId.value } 104 | }) 105 | }) 106 | 107 | return ( 108 | <> 109 | ( 112 | <>Loading Item 113 | )} 114 | onResolved={(data: any) => ( 115 | <>{ data.itemById.title } 116 | )} 117 | onRejected={(error) => ( 118 | <>Error fetching item: {error} 119 | )} 120 | /> 121 | 122 | ) 123 | }) 124 | ``` 125 | 126 | ### Manual refetching 127 | 128 | In many cases, we want to refetch the query after a mutation is called, or when something happens, like a button click. In these cases, we wouldn't watch the query variables, instead we will watch a "refetch counter" that we create. 129 | 130 | So, we'll create a store for refetch count, and start watching it in `useResource$`. To trigger refetch, we just need to increment that counter. 131 | 132 | ```jsx 133 | import { useQuery } from 'qwikql' 134 | import { ITEM_BY_ID } from '~/graphql/queries' 135 | import { useStore } from '@builder.io/qwik' 136 | 137 | export default component$(() => { 138 | // Create the refetch counter 139 | const refetchCount = useStore({ value: 0 }) 140 | 141 | const { executeQuery$ } = useQuery(ITEM_BY_ID) 142 | 143 | const item = useResource$(async ({ track }) => { 144 | track(refetchCount, 'value') 145 | return await executeQuery$({ 146 | variables: { itemId: 'example-item-id' } 147 | }) 148 | }) 149 | 150 | return ( 151 | <> 152 | // Refetch on this button click 153 | 160 | 161 | ( 164 | <>Loading Item 165 | )} 166 | onResolved={(data: any) => ( 167 | <>{ data.itemById.title } 168 | )} 169 | onRejected={(error) => ( 170 | <>Error fetching item: {error} 171 | )} 172 | /> 173 | 174 | ) 175 | }) 176 | ``` 177 | 178 | ## Mutations 179 | 180 | QwikQL provides `useMutation(MUTATION)` hook for GraphQL mutations. It takes the mutation as a parameter, and it returns `{ mutate$, result }`. 181 | 182 | `mutate$(variables)` is a QRL function that takes a variables object to execute the mutation. 183 | 184 | `result` is a store object that contains these variables: `{ data, loading, error }`. 185 | 186 | Here's an example: 187 | 188 | ```jsx 189 | import { useMutation } from 'qwikql' 190 | import { gql } from 'graphql-request' 191 | 192 | export const ADD_ITEM = gql` 193 | mutation addItem($input: AddItemInput!) { 194 | addItem(input: $input) { 195 | id 196 | title 197 | } 198 | } 199 | ` 200 | 201 | export default component$(() => { 202 | const { mutate$, result } = useMutation(ADD_ITEM) 203 | 204 | return ( 205 | <> 206 | { result.loading &&
Adding Item...
} 207 | { result.error &&
ERROR: { result.error.message }
} 208 | { 210 | if (event.key === 'Enter') { 211 | const value = (event.target as HTMLInputElement).value 212 | await mutate$({ 213 | input: { 214 | title: value 215 | } 216 | }) 217 | } 218 | }} 219 | /> 220 | 221 | ) 222 | }) 223 | ``` 224 | 225 | ## Setting Headers 226 | 227 | There are two ways to set headers for your GraphQL operations, either directly as a prop to ``: 228 | 229 | ```jsx 230 | 236 | 237 | ``` 238 | 239 | Or you can set it using `useHeaders()` hook function: 240 | 241 | ```jsx 242 | import { useHeaders } from 'qwikql' 243 | 244 | export default component$(() => { 245 | const setHeaders = useHeaders() 246 | setHeader({ 247 | authorization: 'auth-key' 248 | }) 249 | }) 250 | ``` 251 | 252 | The latter is useful when you get the header values later in other components. A common example is setting the `authorization` after reading the token from the cookies. 253 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "qwikql", 3 | "version": "0.0.3", 4 | "description": "A GraphQL Client for Qwik Framework", 5 | "author": "Taha Shashtari (taha@tahazsh.com)", 6 | "main": "./dist/index.qwik.cjs", 7 | "qwik": "./dist/index.qwik.mjs", 8 | "module": "./dist/index.qwik.mjs", 9 | "types": "./dist/types/index.d.ts", 10 | "exports": { 11 | ".": { 12 | "import": "./dist/index.qwik.mjs", 13 | "require": "./dist/index.qwik.cjs" 14 | } 15 | }, 16 | "files": [ 17 | "dist", 18 | "dist/types" 19 | ], 20 | "engines": { 21 | "node": ">=15.0.0" 22 | }, 23 | "private": false, 24 | "license": "MIT", 25 | "type": "module", 26 | "scripts": { 27 | "build": "qwik build", 28 | "build.lib": "vite build --mode lib", 29 | "build.types": "tsc --emitDeclarationOnly", 30 | "fmt": "prettier --write .", 31 | "fmt.check": "prettier --check .", 32 | "lint": "eslint \"src/**/*.ts*\"", 33 | "release": "np", 34 | "qwik": "qwik" 35 | }, 36 | "peerDependencies": { 37 | "@builder.io/qwik": "1.1.5", 38 | "graphql": "^16.6.0", 39 | "graphql-request": "^5.0.0" 40 | }, 41 | "devDependencies": { 42 | "@builder.io/qwik": "1.2.11", 43 | "@types/eslint": "8.44.2", 44 | "@types/node": "^20.6.0", 45 | "@typescript-eslint/eslint-plugin": "6.7.0", 46 | "@typescript-eslint/parser": "6.7.0", 47 | "esinstall": "^1.1.7", 48 | "eslint": "8.49.0", 49 | "eslint-plugin-qwik": "^1.2.11", 50 | "graphql": "^16.8.0", 51 | "graphql-request": "^6.1.0", 52 | "node-fetch": "3.3.2", 53 | "np": "8.0.4", 54 | "prettier": "3.0.3", 55 | "typescript": "5.2.2", 56 | "vite": "4.4.9" 57 | }, 58 | "repository": { 59 | "type": "git", 60 | "url": "https://github.com/TahaSh/qwikql.git" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/contexts.ts: -------------------------------------------------------------------------------- 1 | import { createContextId, QRL } from '@builder.io/qwik' 2 | 3 | export const QwikqlURLContext = createContextId<{ url: string }>('qwikql.url') 4 | export const QwikqlTimeoutContext = createContextId<{ timeout: number }>( 5 | 'qwikql.timeout' 6 | ) 7 | export const QwikqlRequestContextContext = createContextId<{ 8 | headers: Record 9 | }>('qwikql.requestContext') 10 | export const QwikqlSetHeadersContext = 11 | createContextId) => void>>( 12 | 'qwikql.setHeaders' 13 | ) 14 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export { QwikQL } from './qwikql-component' 2 | export { useMutation } from './useMutation' 3 | export { useQuery } from './useQuery' 4 | export { useHeaders } from './useHeaders' 5 | -------------------------------------------------------------------------------- /src/qwikql-component.tsx: -------------------------------------------------------------------------------- 1 | import { 2 | $, 3 | component$, 4 | Slot, 5 | useContextProvider, 6 | useStore 7 | } from '@builder.io/qwik' 8 | import { 9 | QwikqlRequestContextContext, 10 | QwikqlSetHeadersContext, 11 | QwikqlTimeoutContext, 12 | QwikqlURLContext 13 | } from './contexts' 14 | 15 | interface QwikQLProps { 16 | url: string 17 | timeout?: number 18 | headers?: Record 19 | } 20 | 21 | export const QwikQL = component$((props: QwikQLProps) => { 22 | if (!props.url) { 23 | throw new Error('url prop is missing in QwikQL') 24 | } 25 | 26 | const context = useStore({ headers: props.headers || {} }) 27 | 28 | useContextProvider(QwikqlURLContext, { url: props.url }) 29 | useContextProvider(QwikqlRequestContextContext, context) 30 | useContextProvider(QwikqlTimeoutContext, { timeout: props.timeout }) 31 | useContextProvider( 32 | QwikqlSetHeadersContext, 33 | $((headers) => { 34 | context.headers = headers 35 | }) 36 | ) 37 | return 38 | }) 39 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export interface QwikqlError { 2 | message: string 3 | } 4 | -------------------------------------------------------------------------------- /src/useHeaders.ts: -------------------------------------------------------------------------------- 1 | import { useContext } from '@builder.io/qwik' 2 | import { QwikqlSetHeadersContext } from './contexts' 3 | 4 | export const useHeaders = () => useContext(QwikqlSetHeadersContext) 5 | -------------------------------------------------------------------------------- /src/useMutation.ts: -------------------------------------------------------------------------------- 1 | import { $, useContext, useStore } from '@builder.io/qwik' 2 | import { request, RequestDocument } from 'graphql-request' 3 | import { 4 | QwikqlRequestContextContext, 5 | QwikqlTimeoutContext, 6 | QwikqlURLContext 7 | } from './contexts' 8 | import { toQwikqlError } from './util/toQwikqlError' 9 | 10 | interface MutationStore { 11 | data: any 12 | loading: boolean 13 | error: { message: string } | null 14 | } 15 | 16 | export const useMutation = (mutation: RequestDocument) => { 17 | const url = useContext(QwikqlURLContext).url 18 | const requestContext = useContext(QwikqlRequestContextContext) 19 | const timeout = useContext(QwikqlTimeoutContext).timeout 20 | 21 | const mutationAsString = mutation?.toString() 22 | const result = useStore({ 23 | data: undefined, 24 | loading: false, 25 | error: null 26 | }) 27 | 28 | const mutate$ = $(async (variables: Record) => { 29 | result.loading = true 30 | let controller: AbortController | undefined, timeoutId 31 | if (timeout) { 32 | controller = new AbortController() 33 | timeoutId = setTimeout(() => { 34 | controller!.abort() 35 | }, timeout) 36 | } 37 | 38 | try { 39 | result.data = await request({ 40 | url, 41 | document: mutationAsString, 42 | variables, 43 | requestHeaders: requestContext.headers, 44 | signal: controller?.signal 45 | }) 46 | } catch (error) { 47 | result.error = toQwikqlError(error) 48 | } finally { 49 | if (timeoutId) clearTimeout(timeoutId) 50 | result.loading = false 51 | } 52 | }) 53 | 54 | return { mutate$, result } 55 | } 56 | -------------------------------------------------------------------------------- /src/useQuery.ts: -------------------------------------------------------------------------------- 1 | import { useContext, $ } from '@builder.io/qwik' 2 | import { request, RequestDocument } from 'graphql-request' 3 | import { 4 | QwikqlRequestContextContext, 5 | QwikqlTimeoutContext, 6 | QwikqlURLContext 7 | } from './contexts' 8 | import { toQwikqlError } from './util/toQwikqlError' 9 | 10 | interface QueryConfig { 11 | variables?: Record 12 | } 13 | 14 | export const useQuery = (query: RequestDocument) => { 15 | const queryAsString = query.toString() 16 | const url = useContext(QwikqlURLContext).url 17 | const requestContext = useContext(QwikqlRequestContextContext) 18 | const timeout = useContext(QwikqlTimeoutContext).timeout 19 | 20 | const executeQuery$ = $(async (queryConfig: Partial = {}) => { 21 | let controller: AbortController | undefined, timeoutId 22 | if (timeout) { 23 | controller = new AbortController() 24 | timeoutId = setTimeout(() => { 25 | controller!.abort() 26 | }, timeout) 27 | } 28 | 29 | try { 30 | return await request({ 31 | url, 32 | document: queryAsString, 33 | variables: queryConfig.variables || undefined, 34 | requestHeaders: requestContext.headers, 35 | signal: controller?.signal 36 | }) 37 | } catch (error) { 38 | return Promise.reject(toQwikqlError(error)) 39 | } finally { 40 | if (timeoutId) clearTimeout(timeoutId) 41 | } 42 | }) 43 | 44 | return { executeQuery$ } 45 | } 46 | -------------------------------------------------------------------------------- /src/util/toQwikqlError.ts: -------------------------------------------------------------------------------- 1 | import { QwikqlError } from '../types' 2 | 3 | export function toQwikqlError(error: unknown): QwikqlError { 4 | if (error instanceof Error) { 5 | let message = error.message 6 | const regExp = /\{\s*"message"/gi 7 | if (regExp.test(message)) { 8 | const matches = message.match(/"message"[^"]*?"(.*?)"/) 9 | message = matches?.[1] || message 10 | } 11 | return { 12 | message 13 | } 14 | } 15 | if (typeof error === 'string') { 16 | return { 17 | message: error 18 | } 19 | } 20 | return { 21 | message: 'error' 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "target": "ES2017", 5 | "module": "ES2020", 6 | "lib": ["es2020", "DOM"], 7 | "jsx": "react-jsx", 8 | "jsxImportSource": "@builder.io/qwik", 9 | "strict": true, 10 | "declaration": true, 11 | "declarationDir": "dist/types", 12 | "resolveJsonModule": true, 13 | "moduleResolution": "node", 14 | "esModuleInterop": true, 15 | "skipLibCheck": true, 16 | "isolatedModules": true, 17 | "types": ["vite/client"] 18 | }, 19 | "include": ["src"] 20 | } 21 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig, rollupVersion } from 'vite' 2 | import { qwikVite } from '@builder.io/qwik/optimizer' 3 | 4 | export default defineConfig(() => { 5 | return { 6 | build: { 7 | target: 'es2020', 8 | lib: { 9 | entry: './src/index.ts', 10 | formats: ['es', 'cjs'], 11 | fileName: (format) => `index.qwik.${format === 'es' ? 'mjs' : 'cjs'}` 12 | }, 13 | rollupOptions: { 14 | external: ['graphql-request', 'graphql'] 15 | }, 16 | outDir: './dist' 17 | }, 18 | plugins: [qwikVite()] 19 | } 20 | }) 21 | --------------------------------------------------------------------------------