Skip to content

Instantly share code, notes, and snippets.

View ziedHamdi's full-sized avatar
💭
Exploring opportunities

Zied Hamdi ziedHamdi

💭
Exploring opportunities
View GitHub Profile
@ziedHamdi
ziedHamdi / _app.js
Created August 27, 2021 13:11
An example of _app.js
import React from 'react';
import {ApolloProvider} from '@apollo/client'
import {useApollo} from '../apollo/client'
import {ThemeProvider} from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import Head from 'next/head';
import theme from '../components/theme';
import PropTypes from 'prop-types';
import {appWithTranslation} from 'next-i18next';
import {Provider} from 'next-auth/client'
@ziedHamdi
ziedHamdi / ssrPage.js
Last active August 27, 2021 13:16
Ssr with Apollo and Next.js: code in page
import {gql, useQuery} from '@apollo/client'
import {Grid} from "@material-ui/core";
import {serverSideTranslations} from 'next-i18next/serverSideTranslations';
import {addApolloState, initializeApollo} from '../apollo/client'
import {getDataFromTree} from "@apollo/client/react/ssr";
export async function getServerSideProps(context) {
const client = initializeApollo({})
// await getDataFromTree(SsrPage) (not working: see bug report)
const query1 = await client.query({query: ComplaintsQuery})
@ziedHamdi
ziedHamdi / install.sh
Created September 15, 2021 10:16
build docker images from git repositories
#!/bin/sh
# GRAPHQL_SERVER_VERSION=$1
# NEXT_SERVER_VERSION=$2
# set variables from .env to system: https://gist.github.com/mihow/9c7f559807069a03e302605691f85572
if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi
# Instructions found in the following links
# https://docs.docker.com/engine/install/ubuntu/
# https://docs.docker.com/engine/install/linux-postinstall/
#: clean previous
sudo apt-get remove docker docker-engine docker.io containerd runc
#: install
sudo apt-get update
sudo apt-get install \
# start: docker-compose up (-d for detached mode)
# stop: docker-compose down
version: "3.9"
services:
redis-server:
container_name: redis-server
image: 'redis:6.2-alpine'
networks:
- weally
@ziedHamdi
ziedHamdi / Dockerfile
Last active September 18, 2021 10:58
Example of docker file
# syntax=docker/dockerfile:1
# docker build . -t graphql:1.0.0
# docker run -d --name graphql_server graphql:1.0.0
# docker ps -a
# docker logs [name]
# docker exec -it [name] /bin/sh
# babel is under node_modules/.bin
# docker rm [name] //removes runtime conainer
# docker rm $(docker ps -a -q) //removes all stopped containers
@ziedHamdi
ziedHamdi / client.js
Created October 4, 2021 11:08
apollo client.js
import {useMemo} from 'react'
import {ApolloClient, HttpLink, gql} from '@apollo/client'
import merge from 'deepmerge'
import {cache} from './cache'
import {isSSR} from "../constants/util";
import isEqual from 'lodash/isEqual'
import logger from "../lib/logger";
export const APOLLO_STATE_PROP_NAME = '__APOLLO_STATE__'
@ziedHamdi
ziedHamdi / index.js
Last active October 4, 2021 11:25
implementation of getServerSideProps in pages/xyz.js files
export async function getServerSideProps(context) {
const session = await getSession(context)
const client = initializeApollo({})
//await getDataFromTree(Index) replaced by manual client.query calls
const cpl = await client.query({
query: ComplaintsQuery, context: {
headers: {'infra-token': session?.infraToken}
}
})
//...
@ziedHamdi
ziedHamdi / [...nextauth].js
Created October 4, 2021 11:40
pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth'
import Providers from 'next-auth/providers'
import jwt from 'jsonwebtoken'
import {TOKEN_SECRET} from "../../../lib/auth";
import logger from "../../../lib/logger";
export default NextAuth({
// Configure one or more authentication providers
providers: [
Providers.Facebook({
@ziedHamdi
ziedHamdi / graphqlSetupVisitor.js
Last active October 4, 2021 12:10
/server/middleware/graphqlSetupVisitor.js adds graphql to express
const graphQlServer = graphqlHTTP(async (req, res, graphQLParams) => {
const infraToken = req.header('infraToken');
let user = null;
if (infraToken) {
try {
var _jwt$verify;
_logger.default.debug("infra token found ");