Skip to content

Commit

Permalink
test(gate): add tests with file upload and apollo client (#529)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squash merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog and understand when they need to update his code and
how.
-->

### Describe your change

Add file upload test using raw fetch and apollo client.

### Motivation and context

Ensure common uses of upload feature to work.

### Migration notes

<!-- Explain HOW users should update their code when required -->

### Checklist

- [x] The change come with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change
  • Loading branch information
michael-0acf4 authored Dec 27, 2023
1 parent 5233c8e commit b04c1b4
Show file tree
Hide file tree
Showing 19 changed files with 3,219 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ repos:
- id: deno-fmt
name: Deno format
language: system
entry: bash -c 'cd typegate && deno fmt --ignore=native,src/typegraphs,tmp && cd ../dev && deno fmt'
entry: bash -c 'cd typegate && deno fmt --ignore=native,src/typegraphs,tmp,tests/e2e/nextjs && cd ../dev && deno fmt'
pass_filenames: false
types:
- ts
files: ^(typegate|dev)/
- id: deno-lint
name: Deno lint
language: system
entry: bash -c 'cd typegate && deno lint --rules-exclude=no-explicit-any --ignore=native,tmp && cd ../dev && deno lint'
entry: bash -c 'cd typegate && deno lint --rules-exclude=no-explicit-any --ignore=native,tmp,tests/e2e/nextjs && cd ../dev && deno lint'
pass_filenames: false
types:
- ts
Expand Down
1 change: 1 addition & 0 deletions dev/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const tsFiles = [
{
root: projectDir,
globstar: true,
exclude: ["typegate/tests/e2e/nextjs"],
},
),
].map((f: WalkEntry) => f.path);
Expand Down
1 change: 1 addition & 0 deletions libs/xtask/src/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl Test {
"mkdir",
"bash",
"npm",
"pnpm",
]
.into_iter()
.map(str::to_owned)
Expand Down
3 changes: 3 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
Binary file added typegate/tests/e2e/nextjs/apollo/app/favicon.ico
Binary file not shown.
16 changes: 16 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const metadata = {
title: "Next.js",
description: "Generated by Next.js",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
7 changes: 7 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<div>
Hello World
</div>
);
}
4 changes: 4 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;
31 changes: 31 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "apollo",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@apollo/client": "^3.8.8",
"apollo-upload-client": "^18.0.1",
"graphql": "^16.8.1",
"next": "14.0.4",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/apollo-upload-client": "^17.0.5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"eslint": "^8",
"eslint-config-next": "14.0.4",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
39 changes: 39 additions & 0 deletions typegate/tests/e2e/nextjs/apollo/pages/api/apollo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import type { NextApiRequest, NextApiResponse } from "next";
import { ApolloClient, gql, InMemoryCache } from "@apollo/client/index";
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";

type ResponseData = {
message: string;
};

export default function handler(
_req: NextApiRequest,
res: NextApiResponse<ResponseData>,
) {
const client = new ApolloClient({
cache: new InMemoryCache(),
// Does FormData.append stuff and enables File Upload
// Please refer to https://github.com/jaydenseric/apollo-upload-client/blob/master/createUploadLink.mjs
// Note: if link and uri are both provided, link takes precedence
link: createUploadLink({ uri: "http://localhost:7897/apollo" })
});

client.mutate({
mutation: gql`
mutation ($files: [File]!, $prefix: String!) {
uploadMany(files: $files, prefix: $prefix)
}
`,
variables: {
files: [1, 2, 3, 4].map((i) =>
new File([`hello #${i}`], `hello-${i}.txt`, { type: "text/plain" })
),
prefix: "user/",
},
})
.then((out: any) => res.status(200).json({ success: out } as any))
.catch((err: any) => res.status(400).json({ error: err!.message } as any));
}
Loading

0 comments on commit b04c1b4

Please sign in to comment.